from pydantic import Field
from datetime import datetime
from typing import List, Optional, Dict
from uuid import UUID
from pydantic.networks import EmailStr
from src.apps.base.schemas.common import BaseSchema
from src.apps.base.schemas.common import AddressSchema
from src.apps.files.schemas.file_common import FileResponseSchema
from src.apps.users.schemas.user_common import UserSchema
from .business_profile import BusinessProfileSchema
# from src.apps.subscriptions.schemas.common import SubscriptionSchema
from src.apps.merchants.schemas.merchant_payment_history import (
    MerchantPaymentHistorySchema,
)


class MerchantOnboardingProgressSchema(BaseSchema):
    label: str = Field(
        description="Human readable description of the progress. e.g 70%"
    )
    value: int = Field(description="Progress value")
    unit: str = Field(description="Progress value unit")
    unit_symbol: str = Field(description="Mathematical symbol of the unit")


# Shared properties
class MerchantBase(BaseSchema):
    name: Optional[str] = Field(
        description="Name of merchant, inclusive of alphanumeric characters only"
    )
    industry: Optional[str] = Field(description="Merchant's officiating industry name")
    tagline: Optional[str] = Field(description="Merchant tagline")
    email: Optional[EmailStr] = Field(
        description="Contact email of merchant, A valid email address"
    )
    phone: Optional[str] = Field(
        description="Contact phone of merchant, A valid phone number"
    )
    registration_no: Optional[str] = Field(
        description="A registration could contain only alphanumeric characters"
    )
    license_no: Optional[str] = Field(description="License Number of merchant")
    merchant_literal: Optional[str] = Field(
        description="Merchant Literal, just a simple 6 digit code to identify the merchant in a human readable way"
    )


class MerchantUserSchema(BaseSchema):
    user: Optional[UserSchema] = Field(
        description="The user resource associated with this merchant"
    )
    is_owner: Optional[bool] = Field(
        description="Is this user the owner of the merchant account?"
    )


class MerchantSchema(MerchantBase):
    id: int = Field(description="Id of merchant resource")
    merchant_id: str = Field(
        description="Unique Id of merchant resource, typically has the form 'mer_xxxx'"
    )
    uuid: UUID = Field(description="System generated uuid of merchant")
    tilled_account_id: Optional[str] = Field(description="Tilled account id of user")
    uin: Optional[str] = Field(
        description="Universal Identification Number of merchant"
    )
    is_active: Optional[bool] = Field(
        description="Activation status of merchant, false denotes that the merchant is inactive"
    )
    is_verified: Optional[bool] = Field(
        description="Verification status of merchant, false denotes that the merchant is unverified"
    )
    is_onboarded: Optional[bool] = Field(
        description="Onboarding status of merchant, false denotes that the merchant has not completed onboarding"
    )
    created_at: Optional[datetime] = Field(
        description="Created date and time as unix timestamp"
    )
    updated_at: Optional[datetime] = Field(
        description="Updated date and time as unix timestamp"
    )
    deleted_at: Optional[datetime] = Field(
        description="Deleted date and time as unix timestamp"
    )
    owner: Optional[UserSchema] = Field(
        description="Administrator of this merchant account"
    )
    addresses: Optional[List[AddressSchema]] = Field(
        description="Addresses of this merchant account"
    )
    # business_profile: Optional[BusinessProfileSchema] = Field(
    #     description="Business Profile / Onboarding information of this merchant"
    # )
    # onboarding_progress: Optional[MerchantOnboardingProgressSchema] = Field(
    #     description="Onboarding progress of this merchant"
    # )
    brand_logo: Optional[FileResponseSchema] = Field(
        description="Brand logo of merchant"
    )
    logo_url: Optional[str] = Field(description="Brand logo url of merchant")
    active_domain: Optional[str] = Field(description="Active site url of merchant")
    default_address: Optional[AddressSchema] = Field(
        description="Default Address of this merchant account"
    )
    tnc_accepted: Optional[bool] = Field(
        description="Terms & Condition acceptance status of the merchant."
    )
    # TODO: Uncomment when SubscriptionSchema is created
    # subscriptions: Optional[List[SubscriptionSchema]] = Field(
    #     description="Subscription details of the mechant"
    # )

    dob: Optional[datetime] = Field(
        description="User date of birth in formate YYYY/MM/DD"
    )
    status: Optional[str] = Field(
        description="Tilled Onboarding Status of the merchant"
    )
    tilled_customer_id: Optional[str] = Field(description="Tilled customer id of user")
    merchant_payment_history: Optional[List[MerchantPaymentHistorySchema]] = Field(
        description="payments history for this merchant"
    )
    merchant_literal: Optional[str] = Field(
        description="Unique Id of merchant resource, typically has the form 'mer_xxxx'"
    )
    # total_pending_payment: Optional[float] = Field(
    #     description="Total pending payment for this merchant"
    # )
    # total_overdue_payment: Optional[float] = Field(
    #     description="Total overdue payment for this merchant"
    # )
    # total_paid: Optional[float] = Field(
    #     description="Total paid payment for this merchant"
    # )
    avg_volume: Optional[float] = Field(
        description="Avg paid amount for this merchant"
    )
    mtd_volume: Optional[float] = Field(
        description="Avg MTD paid amount for this merchant"
    )


class NestedMerchantSchema(MerchantBase):
    id: int = Field(description="Id of merchant resource")
    merchant_id: str = Field(
        description="Unique Id of merchant resource, typically has the form 'mer_xxxx'"
    )
    uuid: UUID = Field(description="System generated uuid of merchant")
    uin: Optional[str] = Field(
        description="Universal Identification Number of merchant"
    )
    brand_logo: Optional[FileResponseSchema] = Field(
        description="Brand logo of merchant"
    )
    logo_url: Optional[str] = Field(description="Brand logo url of merchant")
    active_domain: Optional[str] = Field(description="Active site url of merchant")
    dob: Optional[datetime] = Field(
        description="User date of birth in formate YYYY/MM/DD"
    )
    default_address: Optional[AddressSchema] = Field(
        description="Default Address of this merchant account"
    )
    is_active: Optional[bool] = Field(
        description="Activation status of merchant, false denotes that the merchant is inactive"
    )
    is_verified: Optional[bool] = Field(
        description="Verification status of merchant, false denotes that the merchant is unverified"
    )
    is_onboarded: Optional[bool] = Field(
        description="Onboarding status of merchant, false denotes that the merchant has not completed onboarding"
    )
    tnc_accepted: Optional[bool] = Field(
        description="Terms & Condition acceptance status of the merchant."
    )
    preferences: Optional[Dict] = Field(description="merchant global preferences")
    status: Optional[str] = Field(
        description="Tilled Onboarding Status of the merchant"
    )
    created_at: Optional[datetime] = Field(
        description="Created date and time as unix timestamp"
    )
    updated_at: Optional[datetime] = Field(
        description="Updated date and time as unix timestamp"
    )


class MerchantContactBase(BaseSchema):
    name: Optional[str] = Field(description="Name of merchant contact", default=None)
    email: Optional[str] = Field(description="Email of merchant contact", default=None)
    phone: Optional[str] = Field(
        description="Phone number of merchant contact", default=None
    )
    contact_type: Optional[str] = Field(
        description="Contact type of merchant contact", default=None
    )
    is_active: Optional[bool] = Field(
        description="Merchant contact details is active/not active"
    )
    title: Optional[str] = Field(description="Website of the customer contact")
    dob: Optional[datetime] = Field(description="DOB of the Customer contact")
    website: Optional[str] = Field(description="Website of the customer contact")
    timezone: Optional[str] = Field(
        description="Selected timezone of the customer contact"
    )


class MerchantLocationBase(BaseSchema):
    name: Optional[str] = Field(description="Name of merchant contac", default=None)
    email: Optional[str] = Field(description="Email of merchant contact", default=None)
    phone: Optional[str] = Field(
        description="Phone number of merchant contact", default=None
    )
    location_type: Optional[str] = Field(
        description="Contact type of merchant contact", default=None
    )
    address: Optional[AddressSchema] = Field(
        description="Address relationship field", default=None
    )
    is_active: Optional[bool] = Field(
        description="Merchant location details is active/not active"
    )


class AdminMerchantSchema(BaseSchema):
    id: Optional[int] = Field(description="Id of merchant resource")
    merchant_id: Optional[str] = Field(
        description="Unique Id of merchant resource, typically has the form 'mer_xxxx'"
    )
    uuid: Optional[UUID] = Field(description="System generated uuid of merchant")
    tilled_account_id: Optional[str] = Field(description="Tilled account id of user")
    uin: Optional[str] = Field(
        description="Universal Identification Number of merchant"
    )
    is_active: Optional[bool] = Field(
        description="Activation status of merchant, false denotes that the merchant is inactive"
    )
    is_verified: Optional[bool] = Field(
        description="Verification status of merchant, false denotes that the merchant is unverified"
    )
    is_onboarded: Optional[bool] = Field(
        description="Onboarding status of merchant, false denotes that the merchant has not completed onboarding"
    )
    created_at: Optional[datetime] = Field(
        description="Created date and time as unix timestamp"
    )
    updated_at: Optional[datetime] = Field(
        description="Updated date and time as unix timestamp"
    )
    deleted_at: Optional[datetime] = Field(
        description="Deleted date and time as unix timestamp"
    )
    owner: Optional[UserSchema] = Field(
        description="Administrator of this merchant account"
    )
    addresses: Optional[List[AddressSchema]] = Field(
        description="Addresses of this merchant account"
    )
    business_profile: Optional[BusinessProfileSchema] = Field(
        description="Business Profile / Onboarding information of this merchant"
    )
    onboarding_progress: Optional[MerchantOnboardingProgressSchema] = Field(
        description="Onboarding progress of this merchant"
    )
    brand_logo: Optional[FileResponseSchema] = Field(
        description="Brand logo of merchant"
    )
    logo_url: Optional[str] = Field(description="Brand logo url of merchant")
    active_domain: Optional[str] = Field(description="Active site url of merchant")
    default_address: Optional[AddressSchema] = Field(
        description="Default Address of this merchant account"
    )
    tnc_accepted: Optional[bool] = Field(
        description="Terms & Condition acceptance status of the merchant."
    )
    # subscriptions: Optional[List[SubscriptionSchema]] = Field(
    #     description="Subscription details of the mechant"
    # )
    dob: Optional[datetime] = Field(
        description="User date of birth in formate YYYY/MM/DD"
    )
    status: Optional[str] = Field(
        description="Tilled Onboarding Status of the merchant"
    )
    tilled_customer_id: Optional[str] = Field(description="Tilled customer id of user")
    merchant_payment_history: Optional[List[MerchantPaymentHistorySchema]] = Field(
        description="payments history for this merchant"
    )
    merchant_literal: Optional[str] = Field(
        description="Unique Id of merchant resource, typically has the form 'mer_xxxx'"
    )
    avg_vol: Optional[float] = Field(
        default=None,
        description="Average amount of all successful transactions for this merchant.",
    )
    mtd_vol: Optional[float] = Field(
        default=None,
        description="Month-to-date average transaction amount for this merchant.",
    )
