from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime


class ChainCreateSchema(BaseModel):
    chain_name: str = Field(
        ..., description="Name of the chain", min_length=1, max_length=100
    )
    profit_percentage: Optional[float] = Field(
        None, 
        ge=0, 
        le=100, 
        description="Profit percentage for the chain (0-100). Set by superadmin when creating chain."
    )


class ChainSchema(BaseModel):
    chain_id: int = Field(..., description="ID of the chain")
    chain_name: str = Field(..., description="Name of the chain")
    chain_code: str = Field(..., description="Unique chain code (admin managed)")
    profit_percentage: Optional[float] = Field(None, description="Profit percentage for the chain (0-100)")

    class Config:
        from_attributes = True


class ChainCodeUpdate(BaseModel):
    chain_code: str = Field(..., min_length=1, max_length=50, description="New chain code (must be unique)")


class ChainProfitPercentageUpdate(BaseModel):
    profit_percentage: Optional[float] = Field(
        None, 
        ge=0, 
        le=100, 
        description="Profit percentage for the chain (0-100). Can be set to null to remove."
    )


class ChainUpdateSchema(BaseModel):
    """Schema for superadmin to update chain name and/or profit_percentage"""
    chain_name: Optional[str] = Field(
        None, 
        min_length=1, 
        max_length=100, 
        description="New name for the chain (must be unique)"
    )
    profit_percentage: Optional[float] = Field(
        None, 
        ge=0, 
        le=100, 
        description="Profit percentage for the chain (0-100). Can be set to null to remove."
    )
    
    class Config:
        json_schema_extra = {
            "example": {
                "chain_name": "Updated Chain Name",
                "profit_percentage": 15.5
            }
        }