#!/usr/bin/env python
"""
Create remaining missing tables
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.db import connection

print("Creating remaining tables...")
print("=" * 60)

with connection.cursor() as cursor:
    # Create offer_letters table without foreign keys first
    try:
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS offer_letters (
                id CHAR(32) PRIMARY KEY,
                borrower_id CHAR(32),
                loan_application_id CHAR(32),
                offer_date DATE,
                expiry_date DATE,
                loan_amount DECIMAL(15,2),
                interest_rate DECIMAL(5,2),
                duration INT,
                status VARCHAR(20),
                generated_by_id BIGINT,
                created_at DATETIME(6),
                updated_at DATETIME(6)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        """)
        print("✓ Created offer_letters")
    except Exception as e:
        print(f"⚠ offer_letters: {e}")
    
    # Create receipts table
    try:
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS receipts (
                id CHAR(32) PRIMARY KEY,
                receipt_number VARCHAR(50) UNIQUE,
                borrower_id CHAR(32),
                loan_id CHAR(32),
                amount DECIMAL(15,2),
                payment_method VARCHAR(50),
                payment_date DATE,
                generated_by_id BIGINT,
                created_at DATETIME(6),
                updated_at DATETIME(6)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        """)
        print("✓ Created receipts")
    except Exception as e:
        print(f"⚠ receipts: {e}")
    
    # Create sms_templates table
    try:
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS sms_templates (
                id CHAR(32) PRIMARY KEY,
                name VARCHAR(100),
                template_type VARCHAR(50),
                content TEXT,
                is_active TINYINT(1) DEFAULT 1,
                created_at DATETIME(6),
                updated_at DATETIME(6)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        """)
        print("✓ Created sms_templates")
    except Exception as e:
        print(f"⚠ sms_templates: {e}")

print("\n" + "=" * 60)
print("Done!")
