-- ✅ ADD CASE CLOSE COLUMNS TO items TABLE

ALTER TABLE items 
ADD COLUMN berita_acara_no VARCHAR(100) NULL AFTER expires_at,
ADD COLUMN bukti_serah_terima VARCHAR(255) NULL AFTER berita_acara_no,
ADD COLUMN case_closed_at DATETIME NULL AFTER bukti_serah_terima,
ADD COLUMN case_closed_by INT UNSIGNED NULL AFTER case_closed_at,
ADD COLUMN case_closed_notes TEXT NULL AFTER case_closed_by,
ADD CONSTRAINT fk_items_case_closed_by FOREIGN KEY (case_closed_by) REFERENCES users(id);

-- ✅ ADD CASE CLOSE COLUMNS TO archives TABLE

ALTER TABLE archives
ADD COLUMN berita_acara_no VARCHAR(100) NULL AFTER claimed_by,
ADD COLUMN bukti_serah_terima VARCHAR(255) NULL AFTER berita_acara_no;

ALTER TABLE lost_items ADD COLUMN matched_at DATETIME NULL;

-- ✅ ADD INDEX FOR BETTER QUERY PERFORMANCE

CREATE INDEX idx_items_case_closed ON items(case_closed_at, case_closed_by);
CREATE INDEX idx_items_berita_acara ON items(berita_acara_no);

-- ✅ VERIFY CHANGES

SELECT 
    COLUMN_NAME, 
    DATA_TYPE, 
    IS_NULLABLE, 
    COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'items' 
AND COLUMN_NAME IN ('berita_acara_no', 'bukti_serah_terima', 'case_closed_at', 'case_closed_by', 'case_closed_notes')
ORDER BY ORDINAL_POSITION;