-- Migration 012: Create export_jobs table
-- Requirements: 14.4
-- Design: Data Models -> 12. EXPORT JOBS

CREATE TABLE IF NOT EXISTS export_jobs (
    id                  BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    user_id             BIGINT UNSIGNED NOT NULL,
    filter_json         JSON         NOT NULL,
    status              ENUM('QUEUED','PROCESSING','COMPLETED','FAILED')
                            NOT NULL DEFAULT 'QUEUED',
    file_path           VARCHAR(512) NULL,
    row_count           INT          NULL,
    error_message       TEXT         NULL,
    created_at          DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    finished_at         DATETIME(3)  NULL,
    PRIMARY KEY (id),
    KEY idx_ej_user_status (user_id, status, created_at),
    CONSTRAINT fk_ej_user FOREIGN KEY (user_id)
        REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
