#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo Customer Seeding Script for Orders Service
Creates customers for demo template tenants

This script runs as a Kubernetes init job inside the orders-service container.
"""

import asyncio
import uuid
import sys
import os
import json
from datetime import datetime, timezone, timedelta
from pathlib import Path

# Add app to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
import structlog

from app.models.customer import Customer

# Configure logging
logger = structlog.get_logger()

# Base demo tenant IDs
DEMO_TENANT_SAN_PABLO = uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6")  # Individual bakery
DEMO_TENANT_LA_ESPIGA = uuid.UUID("b2c3d4e5-f6a7-48b9-c0d1-e2f3a4b5c6d7")  # Central bakery

# Base reference date for date calculations
BASE_REFERENCE_DATE = datetime(2025, 1, 15, 12, 0, 0, tzinfo=timezone.utc)


def load_customer_data():
    """Load customer data from JSON file"""
    data_file = Path(__file__).parent / "clientes_es.json"
    if not data_file.exists():
        raise FileNotFoundError(f"Customer data file not found: {data_file}")

    with open(data_file, 'r', encoding='utf-8') as f:
        return json.load(f)


def calculate_date_from_offset(offset_days: int) -> datetime:
    """Calculate a date based on offset from BASE_REFERENCE_DATE"""
    return BASE_REFERENCE_DATE + timedelta(days=offset_days)


async def seed_customers_for_tenant(
    db: AsyncSession,
    tenant_id: uuid.UUID,
    tenant_name: str,
    customer_list: list
):
    """Seed customers for a specific tenant"""
    logger.info(f"Seeding customers for: {tenant_name}", tenant_id=str(tenant_id))

    # Check if customers already exist
    result = await db.execute(
        select(Customer).where(Customer.tenant_id == tenant_id).limit(1)
    )
    existing = result.scalar_one_or_none()

    if existing:
        logger.info(f"Customers already exist for {tenant_name}, skipping seed")
        return {"tenant_id": str(tenant_id), "customers_created": 0, "skipped": True}

    count = 0
    for customer_data in customer_list:
        # Calculate dates from offsets
        first_order_date = None
        if "first_order_offset_days" in customer_data:
            first_order_date = calculate_date_from_offset(customer_data["first_order_offset_days"])

        last_order_date = None
        if "last_order_offset_days" in customer_data:
            last_order_date = calculate_date_from_offset(customer_data["last_order_offset_days"])

        # Use strings directly (model doesn't use enums)
        customer_type = customer_data.get("customer_type", "business")
        customer_segment = customer_data.get("customer_segment", "regular")
        is_active = customer_data.get("status", "active") == "active"

        # Create customer (using actual model fields)
        # For San Pablo, use original IDs. For La Espiga, generate new UUIDs
        if tenant_id == DEMO_TENANT_SAN_PABLO:
            customer_id = uuid.UUID(customer_data["id"])
        else:
            # Generate deterministic UUID for La Espiga based on original ID
            base_uuid = uuid.UUID(customer_data["id"])
            # Add a fixed offset to create a unique but deterministic ID
            customer_id = uuid.UUID(int=base_uuid.int + 0x10000000000000000000000000000000)

        customer = Customer(
            id=customer_id,
            tenant_id=tenant_id,
            customer_code=customer_data["customer_code"],
            name=customer_data["name"],
            business_name=customer_data.get("business_name"),
            customer_type=customer_type,
            tax_id=customer_data.get("tax_id"),
            email=customer_data.get("email"),
            phone=customer_data.get("phone"),
            address_line1=customer_data.get("billing_address"),
            city=customer_data.get("billing_city"),
            state=customer_data.get("billing_state"),
            postal_code=customer_data.get("billing_postal_code"),
            country=customer_data.get("billing_country", "España"),
            is_active=is_active,
            preferred_delivery_method=customer_data.get("preferred_delivery_method", "delivery"),
            payment_terms=customer_data.get("payment_terms", "immediate"),
            credit_limit=customer_data.get("credit_limit"),
            discount_percentage=customer_data.get("discount_percentage", 0.0),
            customer_segment=customer_segment,
            priority_level=customer_data.get("priority_level", "normal"),
            special_instructions=customer_data.get("special_instructions"),
            total_orders=customer_data.get("total_orders", 0),
            total_spent=customer_data.get("total_revenue", 0.0),
            average_order_value=customer_data.get("average_order_value", 0.0),
            last_order_date=last_order_date,
            created_at=BASE_REFERENCE_DATE,
            updated_at=BASE_REFERENCE_DATE
        )

        db.add(customer)
        count += 1
        logger.debug(f"Created customer: {customer.name}", customer_id=str(customer.id))

    await db.commit()
    logger.info(f"Successfully created {count} customers for {tenant_name}")

    return {
        "tenant_id": str(tenant_id),
        "customers_created": count,
        "skipped": False
    }


async def seed_all(db: AsyncSession):
    """Seed all demo tenants with customers"""
    logger.info("Starting demo customer seed process")

    # Load customer data
    data = load_customer_data()

    results = []

    # Both tenants get the same customer base
    # (In real scenario, you might want different customer lists)
    result_san_pablo = await seed_customers_for_tenant(
        db,
        DEMO_TENANT_SAN_PABLO,
        "San Pablo - Individual Bakery",
        data["clientes"]
    )
    results.append(result_san_pablo)

    result_la_espiga = await seed_customers_for_tenant(
        db,
        DEMO_TENANT_LA_ESPIGA,
        "La Espiga - Central Bakery",
        data["clientes"]
    )
    results.append(result_la_espiga)

    total_created = sum(r["customers_created"] for r in results)

    return {
        "results": results,
        "total_customers_created": total_created,
        "status": "completed"
    }


async def main():
    """Main execution function"""
    # Get database URL from environment
    database_url = os.getenv("ORDERS_DATABASE_URL")
    if not database_url:
        logger.error("ORDERS_DATABASE_URL environment variable must be set")
        return 1

    # Ensure asyncpg driver
    if database_url.startswith("postgresql://"):
        database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1)

    # Create async engine
    engine = create_async_engine(database_url, echo=False)
    async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)

    try:
        async with async_session() as session:
            result = await seed_all(session)

            logger.info(
                "Customer seed completed successfully!",
                total_customers=result["total_customers_created"],
                status=result["status"]
            )

            # Print summary
            print("\n" + "="*60)
            print("DEMO CUSTOMER SEED SUMMARY")
            print("="*60)
            for tenant_result in result["results"]:
                tenant_id = tenant_result["tenant_id"]
                count = tenant_result["customers_created"]
                skipped = tenant_result.get("skipped", False)
                status = "SKIPPED (already exists)" if skipped else f"CREATED {count} customers"
                print(f"Tenant {tenant_id}: {status}")
            print(f"\nTotal Customers Created: {result['total_customers_created']}")
            print("="*60 + "\n")

            return 0

    except Exception as e:
        logger.error(f"Customer seed failed: {str(e)}", exc_info=True)
        return 1
    finally:
        await engine.dispose()


if __name__ == "__main__":
    exit_code = asyncio.run(main())
    sys.exit(exit_code)
