from typing import List, Optional
from pydantic import Field, ConfigDict
from datetime import datetime
from pydantic.networks import EmailStr
from src.apps.base.schemas.common import BaseSchema
from src.apps.merchants.schemas.business_owner import BusinessOwnerSchema
from src.core.utils.enums import (
    BusinessCategories,
    BusinessLocales,
    BusinessRegions,
    BusinessTypes,
    BusinessYearlyTransactionVolumeTypes,
)
from src.apps.payment_requests.enums import PaymentCurrencies


# Shared properties
class BusinessProfileBase(BaseSchema):
    model_config = ConfigDict(from_attributes=True)
    
    business_name: Optional[str] = Field(default=None, description="The merchant/business name")
    legal_name: Optional[str] = Field(
        default=None,
        description="Registered business legal/trading name (e.g. 'ACME CA')"
    )
    business_type: Optional[BusinessTypes] = Field(default=None, description="Type of business.")
    tax_id: Optional[str] = Field(default=None, description="Company's Tax Identification Number")
    category: Optional[BusinessCategories] = Field(
        default=None,
        description="Category code of the business"
    )
    company_email: Optional[EmailStr] = Field(default=None, description="Email of business.")
    company_phone: Optional[str] = Field(
        default=None,
        description="Customer support phone number of the business. Cannot contain special characters."
    )
    company_website: Optional[str] = Field(default=None, description="Business website")
    region: Optional[BusinessRegions] = Field(
        default=None,
        description="Region for the merchant account"
    )
    locale: Optional[BusinessLocales] = Field(
        default=None,
        description="The locale value used for the merchant account. Values depend on the region."
    )
    currency: Optional[PaymentCurrencies] = Field(
        default=None,
        description="Single currency used for processing and settlement for this merchant account."
    )
    average_transaction_amount: Optional[float] = Field(
        default=None,
        description="Universal Identification Number of BusinessProfile"
    )
    yearly_transaction_volume: Optional[BusinessYearlyTransactionVolumeTypes] = Field(
        default=None,
        description="Estimated yearly volume of transactions of the business."
    )
    yearly_txn_volume: Optional[float] = Field(
        default=None,
        description="Estimated yearly volume of transactions of the business."
    )
    monthly_volume: Optional[float] = Field(
        default=None,
        description="Monthly Volume of BusinessProfile"
    )
    high_transaction_amount: Optional[float] = Field(
        default=None,
        description="Highest Transaction amount of the Business Profile"
    )
    description: Optional[str] = Field(default=None, description="Company's business description")
    purpose: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Provides information about a payment that customers see on their statements. Concatenated with the prefix (shortened descriptor) or statement descriptor that's set on the account to form the complete statement descriptor. Maximum 20 characters for the concatenated descriptor.",
    )
    bank_account_number: Optional[str] = Field(
        default=None,
        max_length=20, 
        description="This is the bank account number"
    )
    bank_routing_number: Optional[str] = Field(
        default=None,
        min_length=9,
        max_length=9,
        description="The sequence of 9-digits used by banks to identify specific financial institutions (i.e. ABA routing number)",
    )


class BusinessProfileSchema(BusinessProfileBase):
    model_config = ConfigDict(from_attributes=True)
    
    id: int = Field(description="Id of BusinessProfile resource")
    business_owners: Optional[List[BusinessOwnerSchema]] = Field(
        default=None,
        description="Registered owners of this business. Only for read-only purposes"
    )
    founding_date: Optional[datetime] = Field(
        default=None,
        description="Founding date of the Business. A valid UNIX timestamp."
    )
    percentage_of_sales_to_other_business: Optional[float] = Field(
        default=None,
        description="Percentage of sales to other business"
    )
    statement_descriptor: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Statement Descriptor Will Appears on credit card statements",
    )
    bank_account_holder_name: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Bank Accountholder Name (Business Name)",
    )
    bank_name: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Bank Name",
    )
    bank_account_type: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Bank accout type",
    )
    bank_account_owner_type: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Owner Type",
    )


class BusinessProfileCreateRequestSchema(BusinessProfileBase):
    model_config = ConfigDict(
        from_attributes=True,
        json_schema_extra={
            "example": {
                "business_name": "Grofers",
                "legal_name": "Grofers",
                "business_type": "CHARITY",
                "tax_id": "123-456-78",
                "category": "CHARITY",
                "company_email": "grofers@yopmail.com",
                "company_phone": "+91 123 456",
            }
        }
    )
    
    business_name: str = Field(description="The merchant/business name")
    legal_name: str = Field(
        description="Registered business legal/trading name (e.g. 'ACME CA')"
    )
    business_type: BusinessTypes = Field(description="Type of business.")
    tax_id: str = Field(description="Company's Tax Identification Number")
    category: BusinessCategories = Field(description="Category code of the business")
    company_phone: str = Field(
        description="Customer support phone number of the business. Cannot contain special characters."
    )


class BusinessProfileUpdateRequestSchema(BusinessProfileBase):
    model_config = ConfigDict(from_attributes=True)


class BusinessProfileBaseV2(BaseSchema):
    model_config = ConfigDict(from_attributes=True)
    
    legal_name: Optional[str] = Field(
        default=None,
        description="Registered business legal/trading name (e.g. 'ACME CA')"
    )
    business_type: Optional[BusinessTypes] = Field(default=None, description="Type of Ownership.")
    tax_id: Optional[str] = Field(default=None, description="Company's Tax Identification Number")
    founding_date: Optional[datetime] = Field(
        default=None,
        description="Founding date of the Business. A valid UNIX timestamp."
    )
    business_name: Optional[str] = Field(
        default=None,
        description="The DBA(Doing business as) name information."
    )
    mail_receive_at: Optional[str] = Field(
        default=None,
        description="Preference for the mail receive at"
    )
    company_email: Optional[EmailStr] = Field(default=None, description="Email of business.")
    company_phone: Optional[str] = Field(
        default=None,
        description="Customer support phone number of the business. Cannot contain special characters."
    )
    history_info: Optional[str] = Field(default=None, description="History info of the business")
    monthly_volume: Optional[float] = Field(
        default=None,
        description="Universal Identification Number of BusinessProfile"
    )
    category: Optional[BusinessCategories] = Field(
        default=None,
        description="Category code of the business"
    )
    fax_number: Optional[str] = Field(default=None, description="fax number of the business")
    company_website: Optional[str] = Field(default=None, description="Business website")
    region: Optional[BusinessRegions] = Field(
        default=None,
        description="Region for the merchant account"
    )
    locale: Optional[BusinessLocales] = Field(
        default=None,
        description="The locale value used for the merchant account. Values depend on the region."
    )
    currency: Optional[PaymentCurrencies] = Field(
        default=None,
        description="Single currency used for processing and settlement for this merchant account."
    )
    average_transaction_amount: Optional[float] = Field(
        default=None,
        description="Universal Identification Number of BusinessProfile"
    )
    high_transaction_amount: Optional[float] = Field(
        default=None,
        description="Highest Transaction amount of the Business Profile"
    )
    yearly_transaction_volume: Optional[BusinessYearlyTransactionVolumeTypes] = Field(
        default=None,
        description="Estimated yearly volume of transactions of the business."
    )
    yearly_txn_volume: Optional[float] = Field(
        default=None,
        description="Estimated yearly volume of transactions of the business."
    )
    description: Optional[str] = Field(default=None, description="Company's business description")
    purpose: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Provides information about a payment that customers see on their statements. Concatenated with the prefix (shortened descriptor) or statement descriptor that's set on the account to form the complete statement descriptor. Maximum 20 characters for the concatenated descriptor.",
    )
    bank_account_number: Optional[str] = Field(
        default=None,
        max_length=20, 
        description="This is the bank account number"
    )
    bank_routing_number: Optional[str] = Field(
        default=None,
        min_length=9,
        max_length=9,
        description="The sequence of 9-digits used by banks to identify specific financial institutions (i.e. ABA routing number)",
    )
    industry: Optional[str] = Field(default=None, description="Company's Industry")
    percentage_of_sales_to_other_business: Optional[float] = Field(
        default=None,
        description="Percentage of sales to other business"
    )
    statement_descriptor: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Statement Descriptor Will Appears on credit card statements",
    )
    bank_account_holder_name: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Bank Accountholder Name (Business Name)",
    )
    bank_name: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Bank Name",
    )
    bank_account_type: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Bank accout type",
    )
    bank_account_owner_type: Optional[str] = Field(
        default=None,
        max_length=20,
        description="Owner Type",
    )


class BusinessProfileSchemaV2(BusinessProfileBaseV2):
    model_config = ConfigDict(from_attributes=True)
    
    id: int = Field(description="Id of BusinessProfile resource")
    business_owners: Optional[List[BusinessOwnerSchema]] = Field(
        default=None,
        description="Registered owners of this business. Only for read-only purposes"
    )
