from sqlalchemy.orm import Session
from app.crud.outlet_ftth import get_ftth_list, get_ftth_detail, get_ftth_list_all

class OutletService:
    @staticmethod
    def get_all_ftth_outlets(db: Session, latitude: float, longitude: float) -> list[dict]:
        ftth_outlet = get_ftth_list_all(db, latitude, longitude)
        if not ftth_outlet:
            raise ValueError("Tidak ada outlet dalam radius 1KM")

        return [{
            "HOMEPASS_ID_PARTNER": row.HOMEPASS_ID_PARTNER,
            "CUSTOMER_ID": row.CUSTOMER_ID,
            "HOMEPASS_STATUS": row.HOMEPASS_STATUS,
            "HOMEPASS_ADDRESS": row.HOMEPASS_ADDRESS,
            "CUSTOMER_NAME": row.CUSTOMER_NAME,
            "HOMEPASS_LATITUDE": str(row.HOMEPASS_LATITUDE) if row.HOMEPASS_LATITUDE else None,
            "HOMEPASS_LONGITUDE": str(row.HOMEPASS_LONGITUDE) if row.HOMEPASS_LONGITUDE else None,
            "distance": round(float(row.distance) * 1000, 2),
        } for row in ftth_outlet]

    @staticmethod
    def get_ftth_list_for_user(db: Session, username: str) -> list[dict]:
        ftth_outlet = get_ftth_list(db, username)
        if not ftth_outlet:
            raise ValueError("data outlet ftth kosong")

        return [
            {
                "KODE": row.KODE,
                "LATITUDE": row.LATITUDE,
                "LONGITUDE": row.LONGITUDE,
                "STATUS": row.KETERANGAN,  
            }
            for row in ftth_outlet
        ]

    @staticmethod
    def get_ftth_outlet_detail(db: Session, kode: str) -> dict:
        data = get_ftth_detail(db, kode)
        if not data:
            raise ValueError("outlet ftth tidak ditemukan")
        return data
