-- Migration: Create Other Payments module tables
-- Run this on the database after verifying types for students.id, classes.id, users.id and academic_sessions.id

CREATE TABLE IF NOT EXISTS other_payment_items (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(150) NOT NULL,
    description TEXT NULL,
    default_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    allow_amount_override TINYINT(1) NOT NULL DEFAULT 0,
    allow_partial_payment TINYINT(1) NOT NULL DEFAULT 0,
    allow_discounts TINYINT(1) NOT NULL DEFAULT 1,
    session_id BIGINT UNSIGNED NULL,
    term VARCHAR(20) NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_by BIGINT UNSIGNED NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_other_payment_items_active (is_active),
    KEY idx_other_payment_items_session (session_id),
    KEY idx_other_payment_items_name (name),
    CHECK (default_amount >= 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS student_other_payment_accounts (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    student_id BIGINT UNSIGNED NOT NULL,
    class_id BIGINT UNSIGNED NOT NULL,
    payment_item_id BIGINT UNSIGNED NOT NULL,
    payment_item_name VARCHAR(150) NOT NULL,
    session_id BIGINT UNSIGNED NULL,
    term VARCHAR(20) NULL,
    amount_due DECIMAL(12,2) NOT NULL,
    status ENUM('unpaid','partial','paid','cancelled') NOT NULL DEFAULT 'unpaid',
    created_by BIGINT UNSIGNED NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_sopa_student (student_id),
    KEY idx_sopa_item (payment_item_id),
    KEY idx_sopa_class (class_id),
    CONSTRAINT fk_sopa_student FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE RESTRICT,
    CONSTRAINT fk_sopa_class FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE RESTRICT,
    CONSTRAINT fk_sopa_item FOREIGN KEY (payment_item_id) REFERENCES other_payment_items(id) ON DELETE RESTRICT,
    CHECK (amount_due >= 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS other_payments (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    payment_account_id BIGINT UNSIGNED NULL,
    student_id BIGINT UNSIGNED NOT NULL,
    class_id BIGINT UNSIGNED NOT NULL,
    payment_item_id BIGINT UNSIGNED NOT NULL,
    payment_item_name VARCHAR(150) NOT NULL,
    session_id BIGINT UNSIGNED NULL,
    term VARCHAR(20) NULL,
    amount_due DECIMAL(12,2) NOT NULL,
    amount_paid DECIMAL(12,2) NOT NULL,
    payment_date DATE NOT NULL,
    payment_method VARCHAR(50) NULL,
    receipt_number VARCHAR(100) NOT NULL,
    request_id VARCHAR(80) NULL,
    reference VARCHAR(150) NULL,
    notes TEXT NULL,
    recorded_by BIGINT UNSIGNED NOT NULL,
    status ENUM('completed','reversed') NOT NULL DEFAULT 'completed',
    reversed_by BIGINT UNSIGNED NULL,
    reversed_at DATETIME NULL,
    reversal_reason TEXT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_other_payment_receipt (receipt_number),
    UNIQUE KEY uq_other_payment_request (recorded_by, request_id),
    KEY idx_other_payments_student (student_id),
    KEY idx_other_payments_class (class_id),
    KEY idx_other_payments_item (payment_item_id),
    KEY idx_other_payments_date (payment_date),
    KEY idx_other_payments_account (payment_account_id),
    CONSTRAINT fk_other_payment_student FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE RESTRICT,
    CONSTRAINT fk_other_payment_class FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE RESTRICT,
    CONSTRAINT fk_other_payment_item FOREIGN KEY (payment_item_id) REFERENCES other_payment_items(id) ON DELETE RESTRICT,
    CONSTRAINT fk_other_payments_account FOREIGN KEY (payment_account_id) REFERENCES student_other_payment_accounts(id) ON DELETE RESTRICT,
    CHECK (amount_due >= 0),
    CHECK (amount_paid >= 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- End migration
