from fastapi import HTTPException
from sqlalchemy.orm import Session
import app.api.customer_location.schemas as schemas
from app.locale.messages import Messages
from app.models.main.customer_location import CustomerLocationBase,TblCustomerLocation
from app.utils.common_utils import calculate_total_household_consumption, fill_and_calculate_total_household_consumption
from app.utils.schemas_utils import CustomResponse

class Customer_location:
    def __init__(self, db: Session, token):
        self.db = db
        self.token = token

    async def create_customer_location(self, request: schemas.customer_location_creat):
        created_user = CustomerLocationBase.model_validate(request.model_dump())
        created_user.total_household_consumption_per_month = calculate_total_household_consumption(created_user)
        TblCustomerLocation.create(created_user, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.CUSTOMER_CREAT)
    
    async def get_customer_location(self, customer_location_id: int):
        catchment = TblCustomerLocation.get_by_id(customer_location_id, self.db)
        if not catchment:
            raise HTTPException(status_code=404,detail=Messages.CUSTOMET_NOT_FOUND)
        return schemas.customerLocationResponse.model_validate(catchment)
    

    async def get_customer_locations_by_group(self, group_id: int):
        customer_locations = (self.db.query(TblCustomerLocation).filter(TblCustomerLocation.group_id == group_id).all())
        return [schemas.customerLocationResponse.model_validate(loc) for loc in customer_locations]
    
    async def get_total_potential_by_group(self, group_id: int) -> schemas.TotalConsumptionResponse:
        locations = (self.db.query(TblCustomerLocation).filter(TblCustomerLocation.group_id == group_id).all())
        if not locations:
            raise HTTPException(status_code=404, detail=Messages.CUSTOMER_GROUP_NOT_FOUND)
        total_consumption = sum(loc.shopping_frequency_per_month * loc.household_consumption_per_month for loc in locations)
        return schemas.TotalConsumptionResponse(group_id=group_id,total_household_consumption_per_month=total_consumption)

    async def update_customer_location(self, request: schemas.customerLocationUpdate):
        if request.customer_location_id is None:
            raise HTTPException(status_code=404, detail=Messages.CUSTOMER_ID_REQUIRED)
        existing = TblCustomerLocation.get_by_id(request.customer_location_id, self.db)
        if not existing:
            raise HTTPException(status_code=404, detail=Messages.CUSTOMET_NOT_FOUND)
        update_data = request.model_dump(exclude_unset=True)
        update_data = fill_and_calculate_total_household_consumption(update_data, existing)
        validated_model = CustomerLocationBase(**update_data)
        assert validated_model.customer_location_id is not None
        TblCustomerLocation.update(validated_model.customer_location_id, validated_model, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.CUSTOMER_UPDATE)
    
    async def delete_customer_location(self, customer_location_id: int):
        deleted = TblCustomerLocation.delete(customer_location_id, self.db)
        if not deleted:
            raise HTTPException(status_code=404,detail=Messages.CUSTOMET_NOT_FOUND)
        return CustomResponse(status="1", message=Messages.CUSTOMER_DELETED)
    
  


    