ALTER TABLE public.faq_items
    ADD COLUMN IF NOT EXISTS resolved_by_confirm boolean DEFAULT false NOT NULL;

UPDATE public.faq_items
SET status = CASE
    WHEN best_reply_id IS NOT NULL THEN 'RESOLVED'
    WHEN EXISTS (
        SELECT 1 FROM public.faq_replies r
        WHERE r.faq_id = faq_items.id AND r.is_deleted = false
    ) THEN 'PROCESSING'
    ELSE 'PENDING'
END,
resolved_by_confirm = false
WHERE status = 'RESOLVED' AND best_reply_id IS NULL;
