-- Add additional metadata columns to ml_models
ALTER TABLE ml_models ADD COLUMN IF NOT EXISTS supported_languages TEXT[] DEFAULT ARRAY['en'];
ALTER TABLE ml_models ADD COLUMN IF NOT EXISTS features JSONB DEFAULT '{}'::jsonb;
ALTER TABLE ml_models ADD COLUMN IF NOT EXISTS fallback_order INT DEFAULT 0;
ALTER TABLE ml_models ADD COLUMN IF NOT EXISTS base_threshold FLOAT;

-- Populate base_threshold from existing params.threshold when available
UPDATE ml_models
SET base_threshold = COALESCE(base_threshold, NULLIF(params->>'threshold', '')::float)
WHERE base_threshold IS NULL;

-- Ensure supported_languages has sane default
UPDATE ml_models
SET supported_languages = ARRAY['en']
WHERE supported_languages IS NULL;

-- Seed / upsert recommended models (Arabic + Multilingual + English)
INSERT INTO ml_models (id, task, name, version, path_reference, active, params, performance_metrics, supported_languages, features, fallback_order, base_threshold, updated_at)
SELECT gen_random_uuid(), 'ner', 'arabic-bert', 'v2', NULL, TRUE,
       '{"max_length":512,"threshold":0.82}'::jsonb,
       '{"f1":0.91,"precision":0.93,"recall":0.89}'::jsonb,
       ARRAY['ar'],
       '{
          "description": "AraBERT Arabic NER tuned للبيانات القانونية والمالية",
          "hf_inference_url": "https://api-inference.huggingface.co/models/qarib/bert-base-qarib-ner",
          "hf_task": "token-classification",
          "entity_map": {
            "EMAIL": "pii.email",
            "email": "pii.email",
            "PHONE": "pii.phone",
            "IBAN": "financial.iban",
            "ACCOUNT": "financial.account",
            "PERSON": "pii.person",
            "DATE": "pii.date"
          },
          "notes": "أعلى دقة للوثائق العربية (المراسلات القانونية، العقود)"
       }'::jsonb,
       1,
       0.82,
       NOW()
WHERE NOT EXISTS (SELECT 1 FROM ml_models WHERE name = 'arabic-bert');

INSERT INTO ml_models (id, task, name, version, path_reference, active, params, performance_metrics, supported_languages, features, fallback_order, base_threshold, updated_at)
SELECT gen_random_uuid(), 'text_classification', 'xlm-roberta-large', 'v1', NULL, TRUE,
       '{"max_length":512,"threshold":0.80}'::jsonb,
       '{"f1":0.89,"precision":0.90,"recall":0.88}'::jsonb,
       ARRAY['ar','en','mixed'],
       '{
          "description": "XLM-R متعدد اللغات لتصنيف حساسية الوثائق العربية/الإنجليزية",
          "hf_inference_url": "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment-latest",
          "hf_task": "text-classification",
          "label_map": {
            "LABEL_0": "sensitive.privileged",
            "LABEL_1": "sensitive.internal",
            "LABEL_2": "public.general"
          },
          "notes": "مناسب للمستندات المختلطة وإعطاء قرار حساسية عام"
       }'::jsonb,
       2,
       0.80,
       NOW()
WHERE NOT EXISTS (SELECT 1 FROM ml_models WHERE name = 'xlm-roberta-large');

INSERT INTO ml_models (id, task, name, version, path_reference, active, params, performance_metrics, supported_languages, features, fallback_order, base_threshold, updated_at)
SELECT gen_random_uuid(), 'text_classification', 'deberta-v3-base', 'v3', NULL, TRUE,
       '{"max_length":512,"threshold":0.83}'::jsonb,
       '{"f1":0.92,"precision":0.93,"recall":0.91}'::jsonb,
       ARRAY['en'],
       '{
          "description": "DeBERTa-v3 لتصنيف الوثائق الإنجليزية عالية الحساسية",
          "hf_inference_url": "https://api-inference.huggingface.co/models/microsoft/deberta-v3-base",
          "hf_task": "text-classification",
          "label_map": {
            "LABEL_0": "sensitive.privileged",
            "LABEL_1": "sensitive.internal",
            "LABEL_2": "public.general"
          },
          "notes": "أفضل دقة للمراسلات والعقود الإنجليزية"
       }'::jsonb,
       3,
       0.83,
       NOW()
WHERE NOT EXISTS (SELECT 1 FROM ml_models WHERE name = 'deberta-v3-base');
