-- Credit control settings
CREATE TABLE IF NOT EXISTS credit_limits (
    id INT AUTO_INCREMENT PRIMARY KEY,
    agency_id BIGINT UNSIGNED NOT NULL,
    client_id BIGINT UNSIGNED DEFAULT NULL,
    branch_id BIGINT UNSIGNED DEFAULT NULL,
    credit_limit DECIMAL(15, 2) DEFAULT 0,
    credit_period_days INT DEFAULT 30,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY unique_client (agency_id, client_id),
    UNIQUE KEY unique_branch (agency_id, branch_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Credit alerts log
CREATE TABLE IF NOT EXISTS credit_alerts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    agency_id BIGINT UNSIGNED NOT NULL,
    client_id BIGINT UNSIGNED DEFAULT NULL,
    branch_id BIGINT UNSIGNED DEFAULT NULL,
    alert_type ENUM('credit_limit', 'payment_due', 'overdue') NOT NULL,
    message TEXT NOT NULL,
    amount DECIMAL(15, 2),
    is_read BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
