"""Brand."""
import os
import shutil
from typing import Optional

from fastapi import Form, HTTPException, UploadFile
from pydantic import Field
from sqlalchemy.orm import Session

from app.api.brand import schemas
from app.api.brand.schemas import BrandCreate, BrandUpdate
from app.dependency.authantication import JWTPayloadSchema
from app.models.main.brand import BrandBase, TblBrand
from app.utils.schemas_utils import CustomResponse


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

    async def create_brand(self, brand_data: BrandCreate, brand_image: UploadFile):
        if not brand_image:
            return CustomResponse(status="0", message="Brand image is required")
        image_filename = brand_image.filename
        image_path = f"uploads/{image_filename}"
        
        with open(image_path, "wb") as buffer:
            shutil.copyfileobj(brand_image.file, buffer)
        create_data=BrandBase.model_validate(brand_data)
        create_data.brand_image = image_path
        TblBrand.create(create_data,self.db)
        self.db.commit()
        return CustomResponse(status="1", message="Brand created successfully", data={"image_url": image_path})
    
    async def update_brand(self, brand_id: int, brand_data: BrandUpdate, brand_image:  UploadFile = Form(None)):
        brand = self.db.query(TblBrand).filter(TblBrand.brand_id == brand_id).first()
        if not brand:
            return CustomResponse(status="0", message="Brand not found")
        if brand_data.brand_name is not None:
            brand.brand_name = brand_data.brand_name
        if brand_data.rationale is not None:
            brand.rationale = brand_data.rationale
        if brand_image:
            image_filename = brand_image.filename
            image_path = f"uploads/{image_filename}"
            with open(image_path, "wb") as buffer:
                shutil.copyfileobj(brand_image.file, buffer)
            if brand.brand_image and os.path.exists(brand.brand_image):
                os.remove(brand.brand_image)
            # if os.path.exists(brand.brand_image):
                # os.remove(brand.brand_image)
            brand.brand_image = image_path
        self.db.commit()
        self.db.refresh(brand)
        return CustomResponse(status="1", message="Brand updated successfully", data={"image_url": brand.brand_image})
    
    async def get_brand_by_id(self, group_id: int):
        brand = self.db.query(TblBrand).filter(TblBrand.group_id == group_id).first()
        if not brand:
            raise HTTPException(status_code=404, detail="Brand not found")
        return brand
    
    async def get_brands_by_group_id(self, group_id: int):
        brands = self.db.query(TblBrand).filter(TblBrand.group_id == group_id).all()
        if not brands:
            raise HTTPException(status_code=404, detail="No brands found for this group")
        return brands


    async def delete_brand(self, brand_id: int):
        deleted = TblBrand.delete(brand_id, self.db)
        if not deleted:
            return CustomResponse(status="-1", message="Brand not found")
        return CustomResponse(status="1", message="Brand deleted successfully")

