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

from fastapi import File, 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 = File(None)):

        image_path = None 
        if brand_image and brand_image.filename:
            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 update_brand(
        self, 
        brand_id: int, 
        brand_data: BrandUpdate, 
        brand_image: UploadFile = Form(None)
    ):
        """
        Updates a brand's data and optionally replaces its image.
        """
        brand = self.db.query(TblBrand).filter(TblBrand.brand_id == brand_id).first()
        
        # 1. Check if brand exists
        if not brand:
            return CustomResponse(status="0", message="Brand not found")

        # 2. Update text fields if provided (Pydantic model fields are assumed to be optional/nullable)
        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
        
        # 3. Handle optional image update
        if brand_image:
            # Check if a file was actually provided (sometimes a boundary is sent without content)
            if brand_image.filename:
                image_filename = brand_image.filename
                image_path = f"uploads/{image_filename}"
                
                # Save the new file
                # Note: For production use, consider async I/O or run in thread pool
                with open(image_path, "wb") as buffer:
                    shutil.copyfileobj(brand_image.file, buffer)
                
                # Delete the old file (only if a new one was successfully saved)
                if brand.brand_image and os.path.exists(brand.brand_image):
                    try:
                        os.remove(brand.brand_image)
                    except OSError as e:
                        # Log the error if file removal fails but don't stop the update
                        print(f"Error deleting old image file {brand.brand_image}: {e}")
                
                # Update the brand image path in the database object
                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")

