CREATE TABLE IF NOT EXISTS complaints (
    id BIGINT NOT NULL AUTO_INCREMENT,
    external_registry_id INT NOT NULL DEFAULT 0,
    external_report_id INT NOT NULL,
    report_date_utc DATETIME(6) NULL,
    gestiona_file_url TEXT NOT NULL,
    gestiona_file_code TEXT NOT NULL,
    gestiona_person_id INT NOT NULL DEFAULT 0,
    tag TEXT NOT NULL,
    status_name TEXT NOT NULL,
    complaint_type TEXT NOT NULL,
    reporter_kind TEXT NOT NULL,
    is_legal_entity TINYINT(1) NOT NULL DEFAULT 0,
    reporter_first_name TEXT NOT NULL,
    reporter_first_surname TEXT NOT NULL,
    reporter_second_surname TEXT NOT NULL,
    reporter_last_name TEXT NOT NULL,
    reporter_business_name TEXT NOT NULL,
    reporter_gender TEXT NOT NULL,
    reporter_document_id TEXT NOT NULL,
    reporter_document_type TEXT NOT NULL,
    reporter_origin_country TEXT NOT NULL,
    subject TEXT NOT NULL,
    accused_party TEXT NOT NULL,
    accused_party_details TEXT NOT NULL,
    complaint_description LONGTEXT NULL,
    reported_to_institution TEXT NOT NULL,
    reported_institution_details TEXT NOT NULL,
    requested_protection TEXT NOT NULL,
    requested_protection_details TEXT NOT NULL,
    information_mode TEXT NOT NULL,
    facts_location TEXT NOT NULL,
    facts_date_utc DATETIME(6) NULL,
    forwarding_authorization TEXT NOT NULL,
    forwarding_personal_data_preference TEXT NOT NULL,
    notification_preference TEXT NOT NULL,
    electronic_notification TEXT NOT NULL,
    online_tracking_preference TEXT NOT NULL,
    postal_notification_preference TEXT NOT NULL,
    email TEXT NOT NULL,
    sms_notification TEXT NOT NULL,
    accepted_terms TINYINT(1) NOT NULL DEFAULT 0,
    comments LONGTEXT NULL,
    phone TEXT NOT NULL,
    address_line TEXT NOT NULL,
    address_road_type TEXT NOT NULL,
    address_number TEXT NOT NULL,
    address_floor TEXT NOT NULL,
    address_door TEXT NOT NULL,
    address_block TEXT NOT NULL,
    address_stair TEXT NOT NULL,
    address_extra TEXT NOT NULL,
    municipality TEXT NOT NULL,
    province TEXT NOT NULL,
    postal_code TEXT NOT NULL,
    country_code TEXT NOT NULL,
    form_fields_json LONGTEXT NULL,
    raw_report_text LONGTEXT NULL,
    is_confidential TINYINT(1) NOT NULL DEFAULT 0,
    is_update TINYINT(1) NOT NULL DEFAULT 0,
    gestiona_procedure_id CHAR(36) NULL,
    gestiona_group_id CHAR(36) NULL,
    display_name TEXT NOT NULL,
    workflow_status TEXT NOT NULL,
    selected_document_name TEXT NULL,
    gestiona_uploaded_at_utc DATETIME(6) NULL,
    gestiona_last_upload_type TEXT NOT NULL,
    gestiona_assigned_group TEXT NOT NULL,
    pending_update_source VARCHAR(256) NOT NULL DEFAULT '',
    is_in_gestiona TINYINT(1) NOT NULL DEFAULT 0,
    is_rejected TINYINT(1) NOT NULL DEFAULT 0,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY uq_complaints_external_report_id (external_report_id),
    KEY ix_complaints_flags (is_update, is_in_gestiona, is_rejected),
    KEY ix_complaints_uploaded_at (gestiona_uploaded_at_utc)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS app_users (
    id BIGINT NOT NULL AUTO_INCREMENT,
    username VARCHAR(256) NOT NULL,
    last_successful_download_at_utc DATETIME(6) NULL,
    last_downloaded_report_moment_utc DATETIME(6) NULL,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY uq_app_users_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS work_groups (
    id BIGINT NOT NULL AUTO_INCREMENT,
    code VARCHAR(32) NOT NULL,
    name VARCHAR(256) NOT NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY uq_work_groups_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO work_groups (code, name)
VALUES
    ('600', 'Asuntos Juridicos y Proteccion a la Persona Denunciante'),
    ('510', 'SDI - Investigacion Entradas')
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    is_active = 1;

CREATE TABLE IF NOT EXISTS app_user_groups (
    app_user_id BIGINT NOT NULL,
    work_group_id BIGINT NOT NULL,
    assigned_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    assigned_by_username VARCHAR(256) NOT NULL DEFAULT '',
    PRIMARY KEY (app_user_id, work_group_id),
    KEY ix_app_user_groups_group (work_group_id),
    CONSTRAINT fk_app_user_groups_user
        FOREIGN KEY (app_user_id) REFERENCES app_users(id)
        ON DELETE CASCADE,
    CONSTRAINT fk_app_user_groups_group
        FOREIGN KEY (work_group_id) REFERENCES work_groups(id)
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS app_user_group_history (
    id BIGINT NOT NULL AUTO_INCREMENT,
    app_user_id BIGINT NOT NULL,
    work_group_id BIGINT NOT NULL,
    action VARCHAR(16) NOT NULL,
    changed_by_username VARCHAR(256) NOT NULL,
    changed_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    KEY ix_app_user_group_history_user (app_user_id),
    KEY ix_app_user_group_history_group (work_group_id),
    CONSTRAINT fk_app_user_group_history_user
        FOREIGN KEY (app_user_id) REFERENCES app_users(id)
        ON DELETE CASCADE,
    CONSTRAINT fk_app_user_group_history_group
        FOREIGN KEY (work_group_id) REFERENCES work_groups(id)
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS inbox_reports (
    id BIGINT NOT NULL AUTO_INCREMENT,
    global_report_uuid CHAR(36) NOT NULL,
    progressive_id INT NULL,
    context_id VARCHAR(128) NULL,
    context_name VARCHAR(256) NULL,
    creation_date_utc DATETIME(6) NULL,
    update_date_utc DATETIME(6) NULL,
    access_date_utc DATETIME(6) NULL,
    last_access_utc DATETIME(6) NULL,
    gl_status VARCHAR(64) NULL,
    gl_label VARCHAR(128) NULL,
    is_updated TINYINT(1) NOT NULL DEFAULT 0,
    first_seen_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    last_seen_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    last_downloaded_at_utc DATETIME(6) NULL,
    last_downloaded_by_user_id BIGINT NULL,
    owner_user_id BIGINT NULL,
    imported_complaint_report_id INT NULL,
    imported_to_store_at_utc DATETIME(6) NULL,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY uq_inbox_reports_uuid (global_report_uuid),
    KEY ix_inbox_reports_progressive (progressive_id),
    KEY ix_inbox_reports_downloaded (last_downloaded_at_utc),
    KEY ix_inbox_reports_owner (owner_user_id),
    CONSTRAINT fk_inbox_reports_last_user
        FOREIGN KEY (last_downloaded_by_user_id) REFERENCES app_users(id)
        ON DELETE SET NULL,
    CONSTRAINT fk_inbox_reports_owner_user
        FOREIGN KEY (owner_user_id) REFERENCES app_users(id)
        ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS user_inbox_reports (
    app_user_id BIGINT NOT NULL,
    inbox_report_id BIGINT NOT NULL,
    first_seen_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    last_seen_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    first_downloaded_at_utc DATETIME(6) NULL,
    last_downloaded_at_utc DATETIME(6) NULL,
    download_count INT NOT NULL DEFAULT 0,
    PRIMARY KEY (app_user_id, inbox_report_id),
    KEY ix_user_inbox_reports_last_downloaded (last_downloaded_at_utc),
    CONSTRAINT fk_user_inbox_reports_user
        FOREIGN KEY (app_user_id) REFERENCES app_users(id)
        ON DELETE CASCADE,
    CONSTRAINT fk_user_inbox_reports_report
        FOREIGN KEY (inbox_report_id) REFERENCES inbox_reports(id)
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS gestiona_upload_history (
    id BIGINT NOT NULL AUTO_INCREMENT,
    external_report_id INT NOT NULL,
    gestiona_file_url TEXT NOT NULL,
    gestiona_file_code VARCHAR(128) NOT NULL DEFAULT '',
    upload_type VARCHAR(64) NOT NULL DEFAULT '',
    assigned_group_code VARCHAR(32) NOT NULL DEFAULT '',
    uploaded_by_username VARCHAR(256) NOT NULL DEFAULT '',
    uploaded_at_utc DATETIME(6) NOT NULL,
    subject TEXT NULL,
    document_names TEXT NULL,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    KEY ix_gestiona_upload_history_report (external_report_id),
    KEY ix_gestiona_upload_history_uploaded_at (uploaded_at_utc),
    KEY ix_gestiona_upload_history_user (uploaded_by_username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS complaint_attachments (
    id BIGINT NOT NULL AUTO_INCREMENT,
    complaint_id BIGINT NOT NULL,
    attachment_type_id INT NOT NULL DEFAULT 0,
    description TEXT NULL,
    attachment_date_utc DATETIME(6) NULL,
    notes TEXT NOT NULL,
    original_file_name VARCHAR(512) NOT NULL,
    content LONGBLOB NOT NULL,
    content_mime_type VARCHAR(128) NOT NULL DEFAULT '',
    content_sha256 CHAR(64) NOT NULL,
    uploaded_to_gestiona TINYINT(1) NOT NULL DEFAULT 0,
    uploaded_at_utc DATETIME(6) NULL,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY uq_attachments_complaint_filename (complaint_id, original_file_name),
    KEY ix_attachments_uploaded (uploaded_to_gestiona, uploaded_at_utc),
    KEY ix_attachments_sha256 (content_sha256),
    CONSTRAINT fk_attachments_complaint
        FOREIGN KEY (complaint_id) REFERENCES complaints(id)
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS complaint_attachment_chunks (
    attachment_id BIGINT NOT NULL,
    chunk_index INT NOT NULL,
    content LONGBLOB NOT NULL,
    created_at_utc DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    PRIMARY KEY (attachment_id, chunk_index),
    CONSTRAINT fk_attachment_chunks_attachment
        FOREIGN KEY (attachment_id) REFERENCES complaint_attachments(id)
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
