-- Profilarr Database Schema
-- This file documents the current database schema after all migrations
-- DO NOT execute this file directly - use migrations instead
-- Last updated: 2025-12-29

-- ==============================================================================
-- TABLE: migrations
-- Purpose: Track applied database migrations
-- Managed by: MigrationRunner (src/db/migrations.ts)
-- ==============================================================================

CREATE TABLE IF NOT EXISTS migrations (
    version INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: arr_instances
-- Purpose: Store configuration for *arr application instances (Radarr, Sonarr, etc.)
-- Migration: 001_create_arr_instances.ts
-- ==============================================================================

CREATE TABLE arr_instances (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    -- Instance identification
    name TEXT NOT NULL UNIQUE,              -- User-friendly name (e.g., "Main Radarr", "4K Sonarr")
    type TEXT NOT NULL,                     -- Instance type: radarr, sonarr, readarr, lidarr, prowlarr

    -- Connection details
    url TEXT NOT NULL,                      -- Base URL (e.g., "http://localhost:7878")
    api_key TEXT NOT NULL,                  -- API key for authentication

    -- Configuration
    tags TEXT,                              -- JSON array of tags (e.g., '["movies","4k"]')
    enabled INTEGER NOT NULL DEFAULT 1,     -- 1=enabled, 0=disabled

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: log_settings
-- Purpose: Store configurable logging settings (singleton pattern with id=1)
-- Migration: 003_create_log_settings.ts, 006_simplify_log_settings.ts
-- ==============================================================================

CREATE TABLE log_settings (
    id INTEGER PRIMARY KEY CHECK (id = 1),

    -- Retention
    retention_days INTEGER NOT NULL DEFAULT 30,

    -- Log Level
    min_level TEXT NOT NULL DEFAULT 'INFO' CHECK (min_level IN ('DEBUG', 'INFO', 'WARN', 'ERROR')),

    -- Enable/Disable
    enabled INTEGER NOT NULL DEFAULT 1,
    file_logging INTEGER NOT NULL DEFAULT 1,
    console_logging INTEGER NOT NULL DEFAULT 1,

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: jobs
-- Purpose: Store job definitions and schedules
-- Migration: 004_create_jobs_tables.ts
-- ==============================================================================

CREATE TABLE jobs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    -- Job identification
    name TEXT NOT NULL UNIQUE,
    description TEXT,

    -- Scheduling
    schedule TEXT NOT NULL,
    enabled INTEGER NOT NULL DEFAULT 1,

    -- Execution tracking
    last_run_at DATETIME,
    next_run_at DATETIME,

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: job_runs
-- Purpose: Store execution history for each job
-- Migration: 004_create_jobs_tables.ts
-- ==============================================================================

CREATE TABLE job_runs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    -- Foreign key to jobs
    job_id INTEGER NOT NULL,

    -- Execution status
    status TEXT NOT NULL CHECK (status IN ('success', 'failure')),

    -- Timing
    started_at DATETIME NOT NULL,
    finished_at DATETIME NOT NULL,
    duration_ms INTEGER NOT NULL,

    -- Output
    error TEXT,
    output TEXT,

    FOREIGN KEY (job_id) REFERENCES jobs(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: backup_settings
-- Purpose: Store configurable backup settings (singleton pattern with id=1)
-- Migration: 005_create_backup_settings.ts
-- ==============================================================================

CREATE TABLE backup_settings (
    id INTEGER PRIMARY KEY CHECK (id = 1),

    -- Backup Configuration
    schedule TEXT NOT NULL DEFAULT 'daily',
    retention_days INTEGER NOT NULL DEFAULT 30,
    enabled INTEGER NOT NULL DEFAULT 1,
    include_database INTEGER NOT NULL DEFAULT 1,
    compression_enabled INTEGER NOT NULL DEFAULT 1,

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: notification_services
-- Purpose: Store notification service configurations (Discord, Slack, Email, etc.)
-- Migration: 007_create_notification_tables.ts
-- ==============================================================================

CREATE TABLE notification_services (
    id TEXT PRIMARY KEY,                        -- UUID

    -- Service identification
    name TEXT NOT NULL,                         -- User-defined: "Main Discord", "Error Alerts"
    service_type TEXT NOT NULL,                 -- 'discord', 'slack', 'email', etc.

    -- Configuration
    enabled INTEGER NOT NULL DEFAULT 0,         -- Master on/off switch
    config TEXT NOT NULL,                       -- JSON blob: { webhook_url: "...", username: "...", ... }
    enabled_types TEXT NOT NULL,                -- JSON array: ["job.backup.success", "job.backup.failed"]

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: notification_history
-- Purpose: Track notification delivery history for auditing and debugging
-- Migration: 007_create_notification_tables.ts
-- ==============================================================================

CREATE TABLE notification_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    -- Foreign key to notification service
    service_id TEXT NOT NULL,

    -- Notification details
    notification_type TEXT NOT NULL,            -- e.g., 'job.backup.success'
    title TEXT NOT NULL,
    message TEXT NOT NULL,
    metadata TEXT,                              -- JSON blob for additional context

    -- Delivery status
    status TEXT NOT NULL CHECK (status IN ('success', 'failed')),
    error TEXT,                                 -- Error message if status = 'failed'

    -- Timing
    sent_at DATETIME DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (service_id) REFERENCES notification_services(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: database_instances
-- Purpose: Store linked Profilarr Compliant Database (PCD) repositories
-- Migration: 008_create_database_instances.ts, 009_add_personal_access_token.ts, 010_add_is_private.ts
-- ==============================================================================

CREATE TABLE database_instances (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    -- Instance identification
    uuid TEXT NOT NULL UNIQUE,                  -- UUID for filesystem storage path
    name TEXT NOT NULL UNIQUE,                  -- User-friendly name (e.g., "Dictionarry DB")

    -- Repository connection
    repository_url TEXT NOT NULL,               -- Git repository URL
    personal_access_token TEXT,                 -- PAT for private repos and push access (Migration 009)
    is_private INTEGER NOT NULL DEFAULT 0,      -- 1=private repo, 0=public (auto-detected, Migration 010)

    -- Local storage
    local_path TEXT NOT NULL,                   -- Path where repo is cloned (data/databases/{uuid})

    -- Sync settings
    sync_strategy INTEGER NOT NULL DEFAULT 0,   -- 0=manual check, >0=auto-check every X minutes
    auto_pull INTEGER NOT NULL DEFAULT 0,       -- 0=notify only, 1=auto-pull updates

    -- Status
    enabled INTEGER NOT NULL DEFAULT 1,         -- 1=enabled, 0=disabled
    last_synced_at DATETIME,                    -- Timestamp of last successful sync

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: upgrade_configs
-- Purpose: Store upgrade configuration per arr instance for automated quality upgrades
-- Migration: 011_create_upgrade_configs.ts, 012_add_upgrade_last_run.ts, 013_add_upgrade_dry_run.ts
-- ==============================================================================

CREATE TABLE upgrade_configs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,

    -- Relationship (one config per arr instance)
    arr_instance_id INTEGER NOT NULL UNIQUE,

    -- Core settings
    enabled INTEGER NOT NULL DEFAULT 0,         -- Master on/off switch
    dry_run INTEGER NOT NULL DEFAULT 0,         -- 1=dry run mode, 0=normal (Migration 013)
    schedule INTEGER NOT NULL DEFAULT 360,      -- Run interval in minutes (default 6 hours)
    filter_mode TEXT NOT NULL DEFAULT 'round_robin', -- 'round_robin' or 'random'

    -- Filters (stored as JSON array of FilterConfig objects)
    filters TEXT NOT NULL DEFAULT '[]',

    -- State tracking
    current_filter_index INTEGER NOT NULL DEFAULT 0, -- For round-robin mode
    last_run_at DATETIME,                            -- When upgrade job last ran (Migration 012)

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (arr_instance_id) REFERENCES arr_instances(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: ai_settings
-- Purpose: Store AI/LLM configuration for commit message generation
-- Migration: 014_create_ai_settings.ts
-- ==============================================================================

CREATE TABLE ai_settings (
    id INTEGER PRIMARY KEY CHECK (id = 1),

    -- Provider settings
    provider TEXT NOT NULL DEFAULT 'openai',    -- 'openai', 'anthropic', etc.
    api_key TEXT,                               -- Encrypted API key
    model TEXT NOT NULL DEFAULT 'gpt-4o-mini',  -- Model identifier

    -- Feature flags
    enabled INTEGER NOT NULL DEFAULT 0,         -- Master on/off switch

    -- Metadata
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ==============================================================================
-- TABLE: arr_sync_quality_profiles
-- Purpose: Store quality profile sync selections (many-to-many)
-- Migration: 015_create_arr_sync_tables.ts
-- ==============================================================================

CREATE TABLE arr_sync_quality_profiles (
    instance_id INTEGER NOT NULL,
    database_id INTEGER NOT NULL,
    profile_id INTEGER NOT NULL,
    PRIMARY KEY (instance_id, database_id, profile_id),
    FOREIGN KEY (instance_id) REFERENCES arr_instances(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: arr_sync_quality_profiles_config
-- Purpose: Store quality profile sync trigger configuration (one per instance)
-- Migration: 015_create_arr_sync_tables.ts, 016_add_should_sync_flags.ts
-- ==============================================================================

CREATE TABLE arr_sync_quality_profiles_config (
    instance_id INTEGER PRIMARY KEY,
    trigger TEXT NOT NULL DEFAULT 'none',       -- 'none', 'manual', 'on_pull', 'on_change', 'schedule'
    cron TEXT,                                  -- Cron expression for schedule trigger
    should_sync INTEGER NOT NULL DEFAULT 0,     -- Flag for pending sync (Migration 016)
    FOREIGN KEY (instance_id) REFERENCES arr_instances(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: arr_sync_delay_profiles
-- Purpose: Store delay profile sync selections (many-to-many)
-- Migration: 015_create_arr_sync_tables.ts
-- ==============================================================================

CREATE TABLE arr_sync_delay_profiles (
    instance_id INTEGER NOT NULL,
    database_id INTEGER NOT NULL,
    profile_id INTEGER NOT NULL,
    PRIMARY KEY (instance_id, database_id, profile_id),
    FOREIGN KEY (instance_id) REFERENCES arr_instances(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: arr_sync_delay_profiles_config
-- Purpose: Store delay profile sync trigger configuration (one per instance)
-- Migration: 015_create_arr_sync_tables.ts, 016_add_should_sync_flags.ts
-- ==============================================================================

CREATE TABLE arr_sync_delay_profiles_config (
    instance_id INTEGER PRIMARY KEY,
    trigger TEXT NOT NULL DEFAULT 'none',       -- 'none', 'manual', 'on_pull', 'on_change', 'schedule'
    cron TEXT,                                  -- Cron expression for schedule trigger
    should_sync INTEGER NOT NULL DEFAULT 0,     -- Flag for pending sync (Migration 016)
    FOREIGN KEY (instance_id) REFERENCES arr_instances(id) ON DELETE CASCADE
);

-- ==============================================================================
-- TABLE: arr_sync_media_management
-- Purpose: Store media management sync configuration (one per instance)
-- Migration: 015_create_arr_sync_tables.ts, 016_add_should_sync_flags.ts
-- ==============================================================================

CREATE TABLE arr_sync_media_management (
    instance_id INTEGER PRIMARY KEY,
    naming_database_id INTEGER,                 -- Database to use for naming settings
    quality_definitions_database_id INTEGER,    -- Database to use for quality definitions
    media_settings_database_id INTEGER,         -- Database to use for media settings
    trigger TEXT NOT NULL DEFAULT 'none',       -- 'none', 'manual', 'on_pull', 'on_change', 'schedule'
    cron TEXT,                                  -- Cron expression for schedule trigger
    should_sync INTEGER NOT NULL DEFAULT 0,     -- Flag for pending sync (Migration 016)
    FOREIGN KEY (instance_id) REFERENCES arr_instances(id) ON DELETE CASCADE
);

-- ==============================================================================
-- INDEXES
-- Purpose: Improve query performance
-- ==============================================================================

-- Jobs indexes (Migration: 004_create_jobs_tables.ts)
CREATE INDEX idx_jobs_enabled ON jobs(enabled);
CREATE INDEX idx_jobs_next_run ON jobs(next_run_at);

-- Job runs indexes (Migration: 004_create_jobs_tables.ts)
CREATE INDEX idx_job_runs_job_id ON job_runs(job_id);
CREATE INDEX idx_job_runs_started_at ON job_runs(started_at);

-- Notification services indexes (Migration: 007_create_notification_tables.ts)
CREATE INDEX idx_notification_services_enabled ON notification_services(enabled);
CREATE INDEX idx_notification_services_type ON notification_services(service_type);

-- Notification history indexes (Migration: 007_create_notification_tables.ts)
CREATE INDEX idx_notification_history_service_id ON notification_history(service_id);
CREATE INDEX idx_notification_history_sent_at ON notification_history(sent_at);
CREATE INDEX idx_notification_history_status ON notification_history(status);

-- Database instances indexes (Migration: 008_create_database_instances.ts)
CREATE INDEX idx_database_instances_uuid ON database_instances(uuid);

-- Upgrade configs indexes (Migration: 011_create_upgrade_configs.ts)
CREATE INDEX idx_upgrade_configs_arr_instance ON upgrade_configs(arr_instance_id);

-- Arr sync indexes (Migration: 015_create_arr_sync_tables.ts)
CREATE INDEX idx_arr_sync_quality_profiles_instance ON arr_sync_quality_profiles(instance_id);
CREATE INDEX idx_arr_sync_delay_profiles_instance ON arr_sync_delay_profiles(instance_id);

-- ==============================================================================
-- TABLE: regex101_cache
-- Purpose: Cache regex101 API responses to avoid redundant fetches
-- Migration: 017_create_regex101_cache.ts
-- ==============================================================================

CREATE TABLE regex101_cache (
    regex101_id TEXT PRIMARY KEY,           -- Versioned ID (e.g., "ABC123/1")
    response TEXT NOT NULL,                 -- Full JSON response with test results
    fetched_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
