PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS companies (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    slug TEXT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT NOT NULL UNIQUE,
    password_hash TEXT NOT NULL,
    role TEXT NOT NULL DEFAULT 'viewer',
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS company_memberships (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    company_id INTEGER NOT NULL,
    role TEXT NOT NULL DEFAULT 'member',
    permissions_json TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (user_id, company_id),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS devices (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    company_id INTEGER NOT NULL,
    external_id TEXT NOT NULL,
    display_name TEXT,
    manufacturer TEXT,
    model TEXT,
    source TEXT NOT NULL DEFAULT 'auto',
    registered_at_ms INTEGER NOT NULL,
    last_seen_at_ms INTEGER NOT NULL,
    meta_json TEXT,
    UNIQUE (company_id, external_id),
    FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS reports (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    company_id INTEGER NOT NULL DEFAULT 1,
    device_id INTEGER,
    report_id TEXT NOT NULL UNIQUE,
    fingerprint TEXT NOT NULL,
    crash_type TEXT NOT NULL,
    generated_at_ms INTEGER NOT NULL,
    received_at_ms INTEGER NOT NULL,
    device_model TEXT,
    app_version TEXT,
    payload_json TEXT NOT NULL,
    tags_json TEXT NOT NULL DEFAULT '[]',
    FOREIGN KEY (company_id) REFERENCES companies(id),
    FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS report_views (
    user_id INTEGER NOT NULL,
    report_id INTEGER NOT NULL,
    viewed_at_ms INTEGER NOT NULL,
    PRIMARY KEY (user_id, report_id),
    FOREIGN KEY (report_id) REFERENCES reports(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS tickets (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    company_id INTEGER NOT NULL DEFAULT 1,
    device_id INTEGER NULL,
    ticket_id TEXT NOT NULL UNIQUE,
    title TEXT NOT NULL,
    brief TEXT,
    body TEXT NOT NULL,
    opened_at_ms INTEGER NOT NULL,
    created_at_ms INTEGER NOT NULL,
    updated_at_ms INTEGER NOT NULL,
    owner_username TEXT NOT NULL DEFAULT '',
    verified_by_username TEXT,
    device_model TEXT,
    app_package TEXT,
    app_version TEXT,
    os_name TEXT,
    os_version TEXT,
    rating INTEGER NOT NULL DEFAULT 0,
    tags_json TEXT NOT NULL DEFAULT '[]',
    payload_json TEXT NOT NULL,
    FOREIGN KEY (company_id) REFERENCES companies(id),
    FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE SET NULL
);

CREATE INDEX IF NOT EXISTS idx_tickets_company ON tickets(company_id);
CREATE INDEX IF NOT EXISTS idx_tickets_opened ON tickets(opened_at_ms DESC);

CREATE INDEX IF NOT EXISTS idx_reports_company ON reports(company_id);
CREATE INDEX IF NOT EXISTS idx_reports_generated ON reports(generated_at_ms DESC);
CREATE INDEX IF NOT EXISTS idx_reports_received ON reports(received_at_ms DESC);
CREATE INDEX IF NOT EXISTS idx_reports_fingerprint ON reports(fingerprint);
CREATE INDEX IF NOT EXISTS idx_reports_company_fingerprint ON reports(company_id, fingerprint);
CREATE INDEX IF NOT EXISTS idx_reports_company_received ON reports(company_id, received_at_ms);
CREATE INDEX IF NOT EXISTS idx_reports_fp_received ON reports(fingerprint, received_at_ms);

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

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

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