CREATE DATABASE IF NOT EXISTS ficha_admissao CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE ficha_admissao;

CREATE TABLE IF NOT EXISTS admin_users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(150) NOT NULL,
    email VARCHAR(190) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    role VARCHAR(20) NOT NULL DEFAULT 'admin',
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS admissions (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    consent TINYINT(1) NOT NULL DEFAULT 0,
    race VARCHAR(50) NOT NULL,
    unit_name VARCHAR(150) NOT NULL,
    job_title VARCHAR(150) NOT NULL,
    full_name VARCHAR(190) NOT NULL,
    father_name VARCHAR(190) NULL,
    mother_name VARCHAR(190) NOT NULL,
    birth_date DATE NOT NULL,
    marital_status VARCHAR(50) NOT NULL,
    is_student TINYINT(1) NOT NULL DEFAULT 0,
    education_level VARCHAR(100) NOT NULL,
    nationality VARCHAR(100) NOT NULL,
    place_of_birth VARCHAR(150) NULL,
    personal_email VARCHAR(190) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    emergency_contact VARCHAR(20) NOT NULL,
    address_line VARCHAR(190) NOT NULL,
    address_number VARCHAR(20) NOT NULL,
    neighborhood VARCHAR(120) NOT NULL,
    zip_code VARCHAR(12) NOT NULL,
    city VARCHAR(120) NOT NULL,
    address_extra VARCHAR(190) NULL,
    state CHAR(2) NOT NULL,
    rg_number VARCHAR(30) NOT NULL,
    rg_issue_date DATE NOT NULL,
    rg_issuer VARCHAR(80) NOT NULL,
    birth_city VARCHAR(120) NOT NULL,
    birth_state CHAR(2) NOT NULL,
    cpf VARCHAR(14) NOT NULL UNIQUE,
    pis_number VARCHAR(20) NOT NULL,
    pis_issue_date DATE NOT NULL,
    has_dependents TINYINT(1) NOT NULL DEFAULT 0,
    dependent_has_disability TINYINT(1) NOT NULL DEFAULT 0,
    uses_transport_voucher TINYINT(1) NOT NULL DEFAULT 0,
    transport_voucher_daily_value DECIMAL(10,2) NULL,
    uniform_size VARCHAR(10) NULL,
    cref_number VARCHAR(30) NULL,
    professor_mei_cnpj VARCHAR(18) NULL,
    employment_function VARCHAR(150) NULL,
    admission_date DATE NULL,
    dismissal_date DATE NULL,
    workload_hours VARCHAR(30) NULL,
    salary_amount DECIMAL(10,2) NULL,
    salary_type VARCHAR(20) NULL,
    bank_agency VARCHAR(30) NULL,
    bank_account VARCHAR(40) NULL,
    status VARCHAR(40) NOT NULL DEFAULT 'recebido',
    internal_notes TEXT NULL,
    lgpd_consent_at DATETIME NULL,
    submitted_ip VARCHAR(45) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_admissions_status (status),
    INDEX idx_admissions_full_name (full_name),
    INDEX idx_admissions_unit (unit_name),
    INDEX idx_admissions_job (job_title)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS dependents (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    admission_id INT UNSIGNED NOT NULL,
    full_name VARCHAR(190) NOT NULL,
    birth_date DATE NOT NULL,
    cpf VARCHAR(14) NOT NULL,
    birth_city VARCHAR(120) NOT NULL,
    has_disability TINYINT(1) NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_dependents_admission FOREIGN KEY (admission_id) REFERENCES admissions(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS academic_infos (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    admission_id INT UNSIGNED NOT NULL,
    internship_nature VARCHAR(120) NOT NULL,
    academic_level VARCHAR(120) NOT NULL,
    institution_name VARCHAR(190) NOT NULL,
    institution_cnpj VARCHAR(18) NOT NULL,
    institution_address VARCHAR(190) NOT NULL,
    institution_neighborhood VARCHAR(120) NOT NULL,
    institution_number VARCHAR(20) NOT NULL,
    institution_zip VARCHAR(12) NOT NULL,
    institution_city VARCHAR(120) NOT NULL,
    institution_state CHAR(2) NOT NULL,
    advisor_name VARCHAR(190) NOT NULL,
    semester VARCHAR(50) NOT NULL,
    course_name VARCHAR(190) NOT NULL,
    registration_number VARCHAR(80) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_academic_admission FOREIGN KEY (admission_id) REFERENCES admissions(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS attachments (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    admission_id INT UNSIGNED NOT NULL,
    label_key VARCHAR(50) NOT NULL,
    original_name VARCHAR(190) NOT NULL,
    stored_name VARCHAR(190) NOT NULL,
    relative_path VARCHAR(255) NOT NULL,
    mime_type VARCHAR(120) NOT NULL,
    size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uniq_admission_label (admission_id, label_key),
    CONSTRAINT fk_attachments_admission FOREIGN KEY (admission_id) REFERENCES admissions(id) ON DELETE CASCADE
) ENGINE=InnoDB;

INSERT INTO admin_users (name, email, password_hash, role, is_active)
VALUES ('Administrador RH','admin@empresa.com','$2y$12$3MVPed3wBmP8cWsSYcV6/eLpDRfwO7seBYb6qYCfU9AfXAyjHUOKe','admin',1)
ON DUPLICATE KEY UPDATE email = VALUES(email), password_hash = VALUES(password_hash), role = VALUES(role), is_active = VALUES(is_active);
