from pydantic import BaseModel, EmailStr, Field,field_validator
from typing import Optional, Dict,List
from datetime import datetime
from src.constants import CompanyUserRoleEnum,DepartmentEnum,ApplicationUserRoleEnum
from src.constants import CompanyPlanEnum,CompanyUserRoleEnum,DepartmentEnum,ApplicationUserRoleEnum



# company user schemas

class AddCompanyUser(BaseModel):
    company_id:str
    full_name: str
    user_email: EmailStr
    user_department: DepartmentEnum
    user_role: CompanyUserRoleEnum


class UpdateCompanyUser(BaseModel):
    user_id: str
    user_email: Optional[EmailStr] = None
    user_department: Optional[DepartmentEnum] = None
    user_role: Optional[CompanyUserRoleEnum] = None
    reset_password: Optional[bool] = None  


class UpdatedCompanyUserResponse(BaseModel):
    user_id: str
    user_email: EmailStr
    user_department: DepartmentEnum
    user_role: CompanyUserRoleEnum
    password: Optional[str] = None  # optional field

    class Config:
        from_attributes = True


class CompanyUserCreate(BaseModel):
    full_name: str
    user_email: EmailStr
    user_department: DepartmentEnum
    user_role: CompanyUserRoleEnum 

    @field_validator("user_department", "user_role", "user_email", mode="before")
    @classmethod
    def lowercase_enum(cls, v):
        if isinstance(v, str):
            return v.lower()
        return v

class CompanyRegistration(BaseModel):
    company_name: str = Field(..., max_length=100)
    legal_name: str = Field(..., max_length=150)
    bio: str = Field(..., max_length=255)
    address: str = Field(..., max_length=255)
    home_country: str = Field(..., max_length=50)
    company_logo_url: str = Field(..., max_length=555)
    has_branches: bool = False                               
    company_email: EmailStr
    company_phone_number: str = Field(..., max_length=255)
    
    
    

    users: List[CompanyUserCreate]

    @field_validator("company_name", "legal_name", "company_email", mode="before")
    @classmethod
    def lowercase_enum(cls, v):
        if isinstance(v, str):
            return v.lower()
        return v



class CompanyRegistrationResponse(BaseModel):
    company_name: str 
    legal_name: str 
    bio: str 
    address: str 
    home_country: str 
    company_logo_url: str 
    has_branches: bool 
    email: EmailStr
    phone_number: str
   
    
    users : List[CompanyUserCreate]

    class Config:
        from_attributes = True




#Application user schemas


class CreateAppUserRequest(BaseModel):
    email: EmailStr
    password: str = Field(min_length=8)
    full_name: str
    role: ApplicationUserRoleEnum
    is_2FA_required: bool = False



class CreateAppUserResponse(BaseModel):
    email: EmailStr
    full_name: str
    role: ApplicationUserRoleEnum
    is_account_active: bool
    is_2FA_required: bool
    is_2FA_enabled: bool
    created_at: datetime

    class Config:
        from_attributes = True

class FileUpload(BaseModel):
    data_type : str