CREATE TABLE IF NOT EXISTS alat_unit_loans (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    loan_number VARCHAR(40) NOT NULL UNIQUE,
    tool_name VARCHAR(180) NOT NULL,
    tool_type ENUM('medis','nonmedis') NOT NULL DEFAULT 'medis',
    serial_number VARCHAR(120) NULL,
    condition_before VARCHAR(120) NULL,
    borrower_unit_id INT NULL,
    borrower_unit_name VARCHAR(160) NULL,
    lender_unit_id INT NULL,
    lender_unit_name VARCHAR(160) NULL,
    borrower_name VARCHAR(160) NOT NULL,
    loan_date DATE NOT NULL,
    planned_return_date DATE NULL,
    status ENUM('dipinjam','dikembalikan') NOT NULL DEFAULT 'dipinjam',
    return_date DATE NULL,
    receiver_name VARCHAR(160) NULL,
    handover_name VARCHAR(160) NULL,
    condition_after VARCHAR(120) NULL,
    notes TEXT NULL,
    created_by INT NULL,
    returned_by INT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_status (status),
    INDEX idx_loan_date (loan_date),
    INDEX idx_borrower_unit (borrower_unit_id),
    INDEX idx_lender_unit (lender_unit_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
