-- Run this once in phpMyAdmin or MySQL console
-- Safe to run even if column already exists (uses IF NOT EXISTS)

ALTER TABLE loans 
    ADD COLUMN IF NOT EXISTS renewed_from INT DEFAULT NULL AFTER status;

ALTER TABLE loans 
    ADD CONSTRAINT fk_renewed_from 
    FOREIGN KEY IF NOT EXISTS (renewed_from) REFERENCES loans(id) ON DELETE SET NULL;

-- Add 'released' status to loans
ALTER TABLE loans MODIFY COLUMN status ENUM('pending', 'approved', 'released', 'paid', 'overdue') DEFAULT 'pending';
