#!/usr/bin/env python
"""
Quick fix script to create the missing M2M table
Run this on production: python fix_m2m_table_quick.py
"""
import os
import sys
import django

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()

from django.db import connection

sql = """
CREATE TABLE IF NOT EXISTS candidates_candidateprofile_preferred_abroad_categories (
    id bigint AUTO_INCREMENT NOT NULL PRIMARY KEY,
    candidateprofile_id bigint NOT NULL,
    abroadjobcategory_id bigint NOT NULL,
    CONSTRAINT candidates_candidateprofile_preferred_abroad_categories_candidateprofile_id_fk 
        FOREIGN KEY (candidateprofile_id) REFERENCES candidates_candidateprofile (id) ON DELETE CASCADE,
    CONSTRAINT candidates_candidateprofile_preferred_abroad_categories_abroadjobcategory_id_fk 
        FOREIGN KEY (abroadjobcategory_id) REFERENCES candidates_abroadjobcategory (id) ON DELETE CASCADE,
    UNIQUE KEY unique_cand_abroad_cat (candidateprofile_id, abroadjobcategory_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
"""

with connection.cursor() as cursor:
    cursor.execute(sql)
    print("✅ Table created successfully!")
