CREATE TABLE import_runs (
    run_id TEXT PRIMARY KEY,
    mode TEXT NOT NULL,
    access_mode TEXT NOT NULL,
    state TEXT NOT NULL,
    total_items INTEGER NOT NULL DEFAULT 0,
    completed_items INTEGER NOT NULL DEFAULT 0,
    active_items INTEGER NOT NULL DEFAULT 0,
    degraded_items INTEGER NOT NULL DEFAULT 0,
    broken_items INTEGER NOT NULL DEFAULT 0,
    warning_items INTEGER NOT NULL DEFAULT 0,
    started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    finished_at TEXT NULL,
    CHECK (mode IN ('strict', 'partial')),
    CHECK (access_mode IN ('subscription', 'self_service')),
    CHECK (state IN ('running', 'completed', 'completed_with_warnings', 'failed', 'cancelled'))
);

CREATE INDEX idx_import_runs_started_at ON import_runs(started_at DESC);
CREATE INDEX idx_import_runs_state ON import_runs(state);
CREATE INDEX idx_import_runs_access_mode ON import_runs(access_mode);

CREATE TABLE import_run_items (
    item_id TEXT PRIMARY KEY,
    run_id TEXT NOT NULL,
    base_url TEXT NOT NULL,
    provider_id TEXT NOT NULL,
    api_key_fingerprint TEXT NOT NULL,
    requested_models_json TEXT NOT NULL DEFAULT '[]',
    raw_models_json TEXT NOT NULL DEFAULT '[]',
    normalized_models_json TEXT NOT NULL DEFAULT '[]',
    canonical_model_families_json TEXT NOT NULL DEFAULT '[]',
    recommended_models_json TEXT NOT NULL DEFAULT '[]',
    resolved_smoke_model TEXT NULL,
    capability_profile_json TEXT NOT NULL DEFAULT '{}',
    current_stage TEXT NOT NULL,
    confirmation_status TEXT NOT NULL,
    access_status TEXT NOT NULL,
    matched_account_state TEXT NOT NULL,
    account_resolution TEXT NOT NULL,
    provision_reused INTEGER NOT NULL DEFAULT 0,
    reused_from_provider_id TEXT NULL,
    reused_from_account_id INTEGER NULL,
    channel_id INTEGER NULL,
    account_id INTEGER NULL,
    retry_count INTEGER NOT NULL DEFAULT 0,
    confirmation_attempts INTEGER NOT NULL DEFAULT 0,
    last_retry_at TEXT NULL,
    next_retry_at TEXT NULL,
    lease_owner TEXT NULL,
    lease_until TEXT NULL,
    advisory_messages_json TEXT NOT NULL DEFAULT '[]',
    last_error_stage TEXT NULL,
    last_error TEXT NULL,
    legacy_batch_id INTEGER NULL,
    legacy_provider_id TEXT NULL,
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_import_run_items_run FOREIGN KEY (run_id) REFERENCES import_runs(run_id) ON DELETE CASCADE,
    CHECK (current_stage IN ('probe', 'provision', 'confirm', 'validate', 'done')),
    CHECK (confirmation_status IN ('pending', 'confirmed', 'advisory', 'failed')),
    CHECK (access_status IN ('unknown', 'active', 'degraded', 'broken')),
    CHECK (matched_account_state IN ('none', 'active', 'disabled', 'deprecated', 'broken')),
    CHECK (account_resolution IN ('created', 'reused', 'reactivated', 'replaced')),
    CHECK (provision_reused IN (0, 1))
);

CREATE INDEX idx_import_run_items_run_id ON import_run_items(run_id);
CREATE INDEX idx_import_run_items_provider_id ON import_run_items(provider_id);
CREATE INDEX idx_import_run_items_api_key_fingerprint ON import_run_items(api_key_fingerprint);
CREATE INDEX idx_import_run_items_current_stage ON import_run_items(current_stage);
CREATE INDEX idx_import_run_items_confirmation_status ON import_run_items(confirmation_status);
CREATE INDEX idx_import_run_items_access_status ON import_run_items(access_status);
CREATE INDEX idx_import_run_items_next_retry_at ON import_run_items(next_retry_at);
CREATE INDEX idx_import_run_items_lease_until ON import_run_items(lease_until);
