#!/usr/bin/env python
"""
Fix Migration Conflicts in Production
"""

import os
import sys
import django
from pathlib import Path

# Set production database credentials
os.environ['DB_NAME'] = 'xygbfpsg_graz'
os.environ['DB_USER'] = 'xygbfpsg_graz'
os.environ['DB_PASSWORD'] = ',qdN3O_!}oC67(]W'
os.environ['DB_HOST'] = 'localhost'
os.environ['DB_PORT'] = '3306'

# Setup Django
BASE_DIR = Path(__file__).resolve().parent
sys.path.insert(0, str(BASE_DIR))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.core.management import call_command

print("="*60)
print("FIXING MIGRATION CONFLICTS")
print("="*60)

# Step 1: Create merge migration
print("\n[1/2] Creating merge migration...")
try:
    call_command('makemigrations', '--merge', '--noinput')
    print("✓ Merge migration created")
except Exception as e:
    print(f"⚠ Merge creation: {e}")

# Step 2: Apply migrations
print("\n[2/2] Applying migrations...")
try:
    call_command('migrate', '--noinput')
    print("✓ Migrations applied")
except Exception as e:
    print(f"✗ Migration failed: {e}")
    sys.exit(1)

print("\n" + "="*60)
print("✅ MIGRATION CONFLICT RESOLVED")
print("="*60)
