-- Scadenzario tables
-- Responsible persons = employees (existing table)
-- Departments = employees.department (varchar field)
-- Notification email = employees.auth_user_id -> auth_users.email

CREATE TABLE IF NOT EXISTS scad_deadlines (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    category VARCHAR(100) NULL,
    topic VARCHAR(500) NOT NULL,
    law_regulation VARCHAR(500) NULL,
    details TEXT NULL,
    recurrence_type VARCHAR(20) NOT NULL DEFAULT 'once',
    due_date DATE NOT NULL,
    check_date DATE NULL,
    document_date DATE NULL,
    notification_days SMALLINT UNSIGNED NOT NULL DEFAULT 7,
    storage_location VARCHAR(500) NULL,
    notes TEXT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    completed_at TIMESTAMP NULL,
    completed_by INT UNSIGNED NULL,
    created_by INT UNSIGNED NOT NULL,
    departments VARCHAR(500) NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_status (status),
    INDEX idx_due_date (due_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS scad_deadline_employee (
    deadline_id INT UNSIGNED NOT NULL,
    employee_id INT UNSIGNED NOT NULL,
    PRIMARY KEY (deadline_id, employee_id),
    CONSTRAINT fk_de_deadline FOREIGN KEY (deadline_id) REFERENCES scad_deadlines(id) ON DELETE CASCADE,
    CONSTRAINT fk_de_employee FOREIGN KEY (employee_id) REFERENCES employees(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS scad_deadline_attachments (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    deadline_id INT UNSIGNED NOT NULL,
    original_name VARCHAR(500) NOT NULL,
    stored_name VARCHAR(500) NOT NULL,
    mime_type VARCHAR(100) NULL,
    size INT UNSIGNED NULL,
    uploaded_by INT UNSIGNED NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_att_deadline FOREIGN KEY (deadline_id) REFERENCES scad_deadlines(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS scad_deadline_histories (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    deadline_id INT UNSIGNED NOT NULL,
    user_id INT UNSIGNED NULL,
    action VARCHAR(50) NOT NULL,
    changes JSON NULL,
    notes TEXT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_hist_deadline (deadline_id),
    CONSTRAINT fk_hist_deadline FOREIGN KEY (deadline_id) REFERENCES scad_deadlines(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS scad_deadline_notifications (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    deadline_id INT UNSIGNED NOT NULL,
    employee_id INT UNSIGNED NOT NULL,
    sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    type VARCHAR(30) NOT NULL,
    INDEX idx_notif_deadline (deadline_id),
    CONSTRAINT fk_notif_deadline FOREIGN KEY (deadline_id) REFERENCES scad_deadlines(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
