CREATE DATABASE IF NOT EXISTS school_fees_db;
USE school_fees_db;

-- 1. Users Table (Izafasha kuri Authentication)
CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL
);

-- 2. Student Table
CREATE TABLE IF NOT EXISTS students (
    StudentID INT AUTO_INCREMENT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    Class VARCHAR(50) NOT NULL
);

-- 3. Fees Table
CREATE TABLE IF NOT EXISTS fees (
    FeeID INT AUTO_INCREMENT PRIMARY KEY,
    Amount DECIMAL(10,2) NOT NULL,
    Description VARCHAR(100) NOT NULL
);

-- 4. Record Table (Kugira ngo tumanitse amashuri n'ibihe/Terms)
CREATE TABLE IF NOT EXISTS records (
    RecordID INT AUTO_INCREMENT PRIMARY KEY,
    Term VARCHAR(50) NOT NULL,
    StudentID INT,
    FeeID INT,
    FOREIGN KEY (StudentID) REFERENCES students(StudentID) ON DELETE CASCADE,
    FOREIGN KEY (FeeID) REFERENCES fees(FeeID) ON DELETE CASCADE
);

-- 5. Payment Table
CREATE TABLE IF NOT EXISTS payments (
    PaymentID INT AUTO_INCREMENT PRIMARY KEY,
    StudentID INT,
    Amount DECIMAL(10,2) NOT NULL,
    Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (StudentID) REFERENCES students(StudentID) ON DELETE CASCADE
);

-- INSERT INITIAL DATA (Kugira ngo database ye kugorana ije ifite amakuru)
INSERT INTO users (username, password) VALUES ('admin', 'admin123'); -- Mu kizamini wakoresha simple pass
INSERT INTO students (Name, Class) VALUES ('Kalisa Rwigema', 'L5 SOD'), ('Mukundwa Ane', 'L4 SOD');
INSERT INTO fees (Amount, Description) VALUES (80000.00, 'Tuition Fee Term 1'), (80000.00, 'Tuition Fee Term 2');
INSERT INTO records (Term, StudentID, FeeID) VALUES ('Term 1', 1, 1), ('Term 1', 2, 1);
INSERT INTO payments (StudentID, Amount) VALUES (1, 50000.00), (2, 80000.00);