# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/organizations.py
import os
import re
import uuid
import hashlib
import logging
from typing import List
from datetime import datetime, timezone
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from app.db.session import get_db
from app.api.deps import get_current_user
from app.schemas.organization import CorpOnboardIn, CorpOnboardResponse
from app.models.marketplace.organization import Organization, OrgType, OrganizationMember
from app.models.identity import User  # JAVÍTVA: Központi Identity modell
from app.core.config import settings

router = APIRouter()
logger = logging.getLogger(__name__)

@router.post("/onboard", response_model=CorpOnboardResponse, status_code=status.HTTP_201_CREATED)
async def onboard_organization(
    org_in: CorpOnboardIn,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Új szervezet (cég/szerviz) rögzítése.
    Automatikusan generál slug-ot és létrehozza a NAS mappa-struktúrát.
    """
    
    # 1. Magyar adószám validáció (XXXXXXXX-Y-ZZ)
    if org_in.country_code == "HU":
        if not re.match(r"^\d{8}-\d-\d{2}$", org_in.tax_number):
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST, 
                detail="Érvénytelen magyar adószám formátum!"
            )

    # 2. Duplikáció ellenőrzés
    stmt_exist = select(Organization).where(Organization.tax_number == org_in.tax_number)
    result_exist = await db.execute(stmt_exist)
    if result_exist.scalar_one_or_none():
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail="Ezzel az adószámmal már regisztráltak céget!"
        )

    # 3. KÖTELEZŐ MEZŐ: folder_slug generálása
    # Mivel az adatbázisban NOT NULL, itt muszáj létrehozni
    temp_slug = hashlib.md5(f"{org_in.tax_number}-{uuid.uuid4()}".encode()).hexdigest()[:12]

    # 4. Mentés
    new_org = Organization(
        full_name=org_in.full_name,
        name=org_in.name,
        display_name=org_in.display_name,
        tax_number=org_in.tax_number,
        reg_number=org_in.reg_number,
        folder_slug=temp_slug, # JAVÍTVA: Kötelező mező beillesztve
        address_zip=org_in.address_zip,
        address_city=org_in.address_city,
        address_street_name=org_in.address_street_name,
        address_street_type=org_in.address_street_type,
        address_house_number=org_in.address_house_number,
        address_hrsz=org_in.address_hrsz,
        country_code=org_in.country_code,
        org_type=OrgType.business,
        status="pending_verification",
        # --- EXPLICIT IDŐBÉLYEGEK A DB HIBA ELKERÜLÉSÉRE ---
        first_registered_at=datetime.now(timezone.utc),
        current_lifecycle_started_at=datetime.now(timezone.utc),
        created_at=datetime.now(timezone.utc),
        subscription_plan="FREE",
        base_asset_limit=1,
        purchased_extra_slots=0,
        notification_settings={},
        external_integration_config={},
        is_ownership_transferable=True
    )
    
    db.add(new_org)
    await db.flush() 

    # 5. TULAJDONOS RÖGZÍTÉSE
    owner_member = OrganizationMember(
        organization_id=new_org.id,
        user_id=current_user.id,
        role="OWNER" # JAVÍTVA: Enum kompatibilis nagybetűs forma
    )
    db.add(owner_member)

    # 6. NAS Mappa létrehozása
    try:
        base_path = getattr(settings, "NAS_STORAGE_PATH", "/mnt/nas/app_data")
        org_path = os.path.join(base_path, "organizations", str(new_org.id))
        os.makedirs(os.path.join(org_path, "documents"), exist_ok=True)
        logger.info(f"NAS mappa kész: {org_path}")
    except Exception as e:
        logger.error(f"NAS hiba: {e}")

    await db.commit()
    await db.refresh(new_org)

    return {"organization_id": new_org.id, "status": new_org.status}

@router.get("/my", response_model=List[dict])
async def get_my_organizations(
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """ A bejelentkezett felhasználóhoz tartozó összes szervezet listázása. """
    stmt = (
        select(Organization)
        .join(OrganizationMember)
        .where(OrganizationMember.user_id == current_user.id)
    )
    result = await db.execute(stmt)
    orgs = result.scalars().all()
    
    # Return full organization details
    return [
        {
            "organization_id": o.id,
            "status": o.status,
            "name": o.name,
            "full_name": o.full_name,
            "display_name": o.display_name,
            "tax_number": o.tax_number,
            "country_code": o.country_code,
            "is_active": o.is_active,
            "is_deleted": o.is_deleted,
            "subscription_plan": o.subscription_plan
        }
        for o in orgs
    ]