"""Copyright 2024 Everlasting Systems and Solutions LLC (www.myeverlasting.net).
All Rights Reserved.

No part of this software or any of its contents may be reproduced, copied, modified or adapted, without the prior written consent of the author, unless otherwise indicated for stand-alone materials.

For permission requests, write to the publisher at the email address below:
office@myeverlasting.net

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
from pydantic import BaseModel, EmailStr, Field, model_validator
from espy_pay.general.enums import TranxEnum,ISPEnum
from typing import Optional, Union
from datetime import datetime
class TranxDto(BaseModel):
    id: Optional[int] = None
    ref: Optional[int] = None # ref is generated by backend
    stripeId: Optional[str] = None
    amount: int = Field(..., gt=2000, description="Amount must be greater than 2000 ")
    description: Optional[str] = None
    currency: str = "NGN"
    stripe_payment_method: Optional[str] = None
    status: Optional[TranxEnum] = TranxEnum.PENDING
    payee_id: Optional[int] = None # Appuser ID field
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    detail: Optional[Union[dict, str]] = None  # Can be dict or string for flexibility
    createdOn: Optional[datetime] = None
    modifiedOn: Optional[datetime] = None
    isp: ISPEnum = ISPEnum.STRIPE

    @model_validator(mode='before')
    def check_email_or_phone(cls, v):
        payee_id = v.get('payee_id')
        first_name = v.get('first_name')
        last_name = v.get('last_name')
        email = v.get('email')
        if not payee_id:
            if not all([first_name, last_name, email]):
                raise ValueError("if payee_id is not provided, first_name, last_name and email must be provided")
        return v
    @model_validator(mode='before')
    def check_stripe(cls, v):
        if v.get('isp') == ISPEnum.STRIPE:
            if not v.get('stripe_payment_method'):
                raise ValueError("stripe_payment_method is required for Stripe transactions")
            v['currency'] = 'USD'
        return v

