-- Fresh MariaDB install for Android Cast crash reporter.
-- Run as root:  mysql -u root -p < sql/schema.mariadb.sql
-- App user needs: SELECT, INSERT, UPDATE, DELETE on androidcast_crashes.*

CREATE DATABASE IF NOT EXISTS androidcast_crashes
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE androidcast_crashes;

CREATE TABLE IF NOT EXISTS companies (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    slug VARCHAR(64) NOT NULL,
    name VARCHAR(128) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_companies_slug (slug)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(64) NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    role ENUM('root', 'platform_admin', 'admin', 'viewer') NOT NULL DEFAULT 'viewer',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_users_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS company_memberships (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id INT UNSIGNED NOT NULL,
    company_id INT UNSIGNED NOT NULL,
    role ENUM('owner', 'admin', 'member') NOT NULL DEFAULT 'member',
    permissions_json LONGTEXT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_membership_user_company (user_id, company_id),
    KEY idx_membership_company (company_id),
    CONSTRAINT fk_membership_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
    CONSTRAINT fk_membership_company FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS devices (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id INT UNSIGNED NOT NULL,
    external_id VARCHAR(128) NOT NULL,
    display_name VARCHAR(256) NULL,
    manufacturer VARCHAR(128) NULL,
    model VARCHAR(128) NULL,
    source ENUM('auto', 'manual', 'barcode') NOT NULL DEFAULT 'auto',
    registered_at_ms BIGINT NOT NULL,
    last_seen_at_ms BIGINT NOT NULL,
    meta_json LONGTEXT NULL,
    UNIQUE KEY uq_devices_company_external (company_id, external_id),
    KEY idx_devices_company (company_id),
    CONSTRAINT fk_devices_company FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS reports (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id INT UNSIGNED NOT NULL DEFAULT 1,
    device_id BIGINT UNSIGNED NULL,
    report_id VARCHAR(128) NOT NULL,
    fingerprint VARCHAR(64) NOT NULL,
    crash_type VARCHAR(32) NOT NULL,
    generated_at_ms BIGINT NOT NULL,
    received_at_ms BIGINT NOT NULL,
    device_model VARCHAR(128) NULL,
    app_version VARCHAR(64) NULL,
    payload_json LONGTEXT NOT NULL,
    tags_json LONGTEXT NOT NULL,
    UNIQUE KEY uq_reports_report_id (report_id),
    KEY idx_reports_company (company_id),
    KEY idx_reports_device (device_id),
    KEY idx_reports_generated (generated_at_ms),
    KEY idx_reports_received (received_at_ms),
    KEY idx_reports_fingerprint (fingerprint),
    KEY idx_reports_fp_received (fingerprint, received_at_ms),
    CONSTRAINT fk_reports_company FOREIGN KEY (company_id) REFERENCES companies (id),
    CONSTRAINT fk_reports_device FOREIGN KEY (device_id) REFERENCES devices (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS report_views (
    user_id INT UNSIGNED NOT NULL,
    report_id BIGINT UNSIGNED NOT NULL,
    viewed_at_ms BIGINT NOT NULL,
    PRIMARY KEY (user_id, report_id),
    CONSTRAINT fk_report_views_report
        FOREIGN KEY (report_id) REFERENCES reports (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

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;

INSERT IGNORE INTO companies (id, slug, name) VALUES (1, 'default', 'Default');

INSERT IGNORE INTO users (id, username, password_hash, role)
VALUES (1, 'admin', '$2y$10$t2JyYCyjBwRxAHfmLwsnG.APoj/in0i6nxpKcQggdl.AkhFJR13o.', 'root');
-- default password: admin — change in production

INSERT IGNORE INTO company_memberships (user_id, company_id, role)
VALUES (1, 1, 'owner');
