from fastapi import APIRouter, Query
from app.core.database import execute_query

router = APIRouter()

@router.get("/search/customers")
async def search_customers(q: str = Query(..., min_length=2)):
    """
    Autocomplete search for customers.
    Returns list of {id, name, cvr_nummer, email}
    """
    sql = """
        SELECT id, name, cvr_number as cvr_nummer, email
        FROM customers
        WHERE (name ILIKE %s OR cvr_number ILIKE %s)
          AND deleted_at IS NULL
          AND is_active = true
        ORDER BY name ASC
        LIMIT 20
    """
    term = f"%{q}%"
    results = execute_query(sql, (term, term))
    return results

@router.get("/search/contacts")
async def search_contacts(q: str = Query(..., min_length=2)):
    """
    Autocomplete search for contacts.
    Returns list of {id, first_name, last_name, email, phone, mobile, user_company, user_company_phone}
    Supports: first name, last name, email, combined "Fornavn Efternavn", phone, mobile.
    """
    sql = """
        SELECT
            c.id,
            c.first_name,
            c.last_name,
            NULLIF(NULLIF(TRIM(c.email), ''), 'null') AS email,
            NULLIF(NULLIF(TRIM(c.phone), ''), 'null') AS phone,
            NULLIF(NULLIF(TRIM(c.mobile), ''), 'null') AS mobile,
            (
                SELECT cu.name
                FROM contact_companies cc
                JOIN customers cu ON cu.id = cc.customer_id
                WHERE cc.contact_id = c.id
                ORDER BY cc.is_primary DESC NULLS LAST, cc.id ASC
                LIMIT 1
            ) AS user_company,
            (
                SELECT NULLIF(NULLIF(TRIM(COALESCE(cu.mobile_phone, cu.phone)), ''), 'null')
                FROM contact_companies cc
                JOIN customers cu ON cu.id = cc.customer_id
                WHERE cc.contact_id = c.id
                ORDER BY cc.is_primary DESC NULLS LAST, cc.id ASC
                LIMIT 1
            ) AS user_company_phone
        FROM contacts c
        WHERE
            c.first_name ILIKE %s
            OR c.last_name ILIKE %s
            OR c.email ILIKE %s
            OR CONCAT(c.first_name, ' ', c.last_name) ILIKE %s
            OR c.phone ILIKE %s
            OR c.mobile ILIKE %s
        ORDER BY c.first_name ASC, c.last_name ASC
        LIMIT 20
    """
    term = f"%{q}%"
    results = execute_query(sql, (term, term, term, term, term, term))
    return results

@router.get("/search/hardware")
async def search_hardware(q: str = Query(..., min_length=2)):
    """
    Autocomplete search for hardware.
    Returns list of {id, brand, model, serial_number, internal_asset_id}
    """
    sql = """
        SELECT id, brand, model, serial_number, internal_asset_id
        FROM hardware_assets
        WHERE 
            (brand ILIKE %s OR 
            model ILIKE %s OR 
            serial_number ILIKE %s OR
            internal_asset_id ILIKE %s)
            AND deleted_at IS NULL
        ORDER BY brand ASC, model ASC
        LIMIT 20
    """
    term = f"%{q}%"
    results = execute_query(sql, (term, term, term, term))
    return results

@router.get("/search/locations")
async def search_locations(q: str = Query(..., min_length=2)):
    """
    Autocomplete search for locations.
    Returns list of {id, name, address_street, address_city}
    """
    sql = """
        SELECT id, name, address_street, address_city
        FROM locations_locations
        WHERE 
            (name ILIKE %s OR 
            address_street ILIKE %s OR 
            address_city ILIKE %s)
            AND deleted_at IS NULL
        ORDER BY name ASC
        LIMIT 20
    """
    term = f"%{q}%"
    results = execute_query(sql, (term, term, term))
    return results
