-- Tickets (tasks / QA / user issues) — same console as crash reports.
-- Run: mysql -u root -p androidcast_crashes < sql/migrations/003_tickets.sql

CREATE TABLE IF NOT EXISTS tickets (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id INT UNSIGNED NOT NULL DEFAULT 1,
    device_id BIGINT UNSIGNED NULL,
    ticket_id VARCHAR(128) NOT NULL,
    title VARCHAR(256) NOT NULL,
    brief TEXT NULL,
    body LONGTEXT NOT NULL,
    opened_at_ms BIGINT NOT NULL,
    created_at_ms BIGINT NOT NULL,
    updated_at_ms BIGINT NOT NULL,
    owner_username VARCHAR(64) NOT NULL DEFAULT '',
    verified_by_username VARCHAR(64) NULL,
    device_model VARCHAR(128) NULL,
    app_package VARCHAR(128) NULL,
    app_version VARCHAR(64) NULL,
    os_name VARCHAR(64) NULL,
    os_version VARCHAR(128) NULL,
    rating TINYINT UNSIGNED NOT NULL DEFAULT 0,
    tags_json LONGTEXT NOT NULL,
    payload_json LONGTEXT NOT NULL,
    UNIQUE KEY uq_tickets_ticket_id (ticket_id),
    KEY idx_tickets_company (company_id),
    KEY idx_tickets_opened (opened_at_ms),
    KEY idx_tickets_updated (updated_at_ms),
    KEY idx_tickets_owner (owner_username),
    CONSTRAINT fk_tickets_company FOREIGN KEY (company_id) REFERENCES companies (id),
    CONSTRAINT fk_tickets_device FOREIGN KEY (device_id) REFERENCES devices (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
