CREATE TABLE provider_accounts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    host_id INTEGER NOT NULL,
    provider_id INTEGER NOT NULL,
    route_id TEXT NOT NULL DEFAULT '',
    shadow_group_id TEXT NOT NULL DEFAULT '',
    host_account_id TEXT NOT NULL,
    key_fingerprint TEXT NOT NULL,
    account_name TEXT NOT NULL DEFAULT '',
    account_status TEXT NOT NULL,
    last_probe_status TEXT NOT NULL DEFAULT '',
    last_probe_at TEXT NOT NULL DEFAULT '',
    disabled_reason TEXT NOT NULL DEFAULT '',
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_provider_accounts_host FOREIGN KEY (host_id) REFERENCES hosts(id) ON DELETE CASCADE,
    CONSTRAINT fk_provider_accounts_provider FOREIGN KEY (provider_id) REFERENCES providers(id) ON DELETE CASCADE,
    CONSTRAINT uq_provider_accounts_host_account UNIQUE (host_id, host_account_id),
    CHECK (account_status IN ('active', 'disabled', 'deprecated', 'broken'))
);

CREATE INDEX idx_provider_accounts_provider_host ON provider_accounts(provider_id, host_id);
CREATE INDEX idx_provider_accounts_status ON provider_accounts(account_status);
CREATE INDEX idx_provider_accounts_route_id ON provider_accounts(route_id);
CREATE INDEX idx_provider_accounts_shadow_group_id ON provider_accounts(shadow_group_id);
