-- Optional machine-translation fields. This does not enable AI analysis.
-- Run after 002_users_and_subscriptions.sql.

BEGIN;

ALTER TABLE news_articles
    ADD COLUMN IF NOT EXISTS language_code VARCHAR(16) NOT NULL DEFAULT 'zh',
    ADD COLUMN IF NOT EXISTS translated_title TEXT,
    ADD COLUMN IF NOT EXISTS translated_summary TEXT,
    ADD COLUMN IF NOT EXISTS translation_status VARCHAR(20) NOT NULL DEFAULT 'not_needed',
    ADD COLUMN IF NOT EXISTS translation_provider VARCHAR(80),
    ADD COLUMN IF NOT EXISTS translated_at TIMESTAMPTZ;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'news_articles_translation_status_check'
    ) THEN
        ALTER TABLE news_articles
            ADD CONSTRAINT news_articles_translation_status_check
            CHECK (translation_status IN ('not_needed', 'pending', 'translated', 'failed', 'skipped'));
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_articles_translation_status
    ON news_articles (translation_status, language_code);

COMMIT;
