from datetime import datetime
from typing import Optional
from pydantic import Field
from src.core.utils.enums import (
    RecurringPaymentTimeIntervals,
)
from src.apps.payment_requests.enums import (
    RecurringPaymentIntervals,
    RecurringPaymentEndTypes,
    RecurringPaymentRepeatTypes,
)
from src.apps.base.schemas.common import BaseSchema


class RecurringPaymentRequestBase(BaseSchema):
    repeat_type: RecurringPaymentRepeatTypes = Field(description="Type of repeatition")
    prorate_first_payment: Optional[bool] = Field(
        description="Enables prorating of the first payment"
    )
    interval: Optional[RecurringPaymentIntervals] = Field(
        default=RecurringPaymentIntervals.MONTH,
        description="Interval of recurring payment",
    )
    interval_value: Optional[int] = Field(
        default=1,
        ge=1,
        description="Value of the interval, like day of month or week etc",
    )
    interval_time: Optional[RecurringPaymentTimeIntervals] = Field(
        description="Time Interval of recurring payment, valid if repeat_type is on time of month"
    )
    start_date: Optional[datetime] = Field(
        description="Billing date and time of recurring payment as unix timestamp"
    )
    end_date: Optional[datetime] = Field(
        description="Recurring end date and time of recurring payment as unix timestamp"
    )
    end_type: Optional[RecurringPaymentEndTypes] = Field(
        description="Type of end preferred for this payment"
    )
    pay_until_count: Optional[int] = Field(
        description="Number of payments to be made before completeion of request, applicable only if end_type is on_count"
    )
    prorate_date: Optional[datetime] = Field(
        description="Prorate date and time of recurring payment as unix timestamp"
    )
    end_date: Optional[datetime] = Field(
        description="Recurring end date and time of recurring payment as unix timestamp"
    )
    # is_autopay: Optional[bool] =  Field(description="Enables auto payment")
    # autopay_type: Optional[RecurringPaymentAutopayTypes] = Field(description="Type of autopay frequency")
    # autopay_interval: Optional[int] = Field(description="Require autopay of payment after number of days from due date")


class RecurringPaymentRequestSchema(RecurringPaymentRequestBase):
    id: int = Field(description="Id of recurring config resource")
    next_due_date: Optional[datetime] = Field(
        description="Recurring next due date and time of recurring payment as unix timestamp"
    )
    next_due_amount: Optional[float] = Field(description="Next due amount")
    prorate_amount: Optional[float] = Field(
        description="Prorate amount of the recurring payment"
    )


class RecurringPaymentRequestCreate(RecurringPaymentRequestBase):
    start_date: datetime = Field(
        description="Billing date and time of recurring payment as unix timestamp"
    )
    # interval_value: float = Field(description="Value of the interval, like day of month or week etc")
    end_type: RecurringPaymentEndTypes = Field(
        description="Type of end preferred for this payment"
    )


class RecurringPaymentRequestUpdate(RecurringPaymentRequestBase):
    next_due_date: Optional[datetime] = Field(
        description="Recurring next due date and time of recurring payment as unix timestamp"
    )
    next_due_amount: Optional[float] = Field(description="Next due amount")
