from sqlalchemy import Column, Integer, String, DateTime, Numeric, Text, Enum, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ..config.database import Base

class PaymentMethod(str, enum.Enum):
    cash = 'cash'
    credit_card = 'credit_card'
    debit_card = 'debit_card'
    bank_transfer = 'bank_transfer'
    e_wallet = 'e_wallet'
    stripe = 'stripe'
    paypal = 'paypal'

class PaymentType(str, enum.Enum):
    full = 'full'
    deposit = 'deposit'
    remaining = 'remaining'

class PaymentStatus(str, enum.Enum):
    pending = 'pending'
    completed = 'completed'
    failed = 'failed'
    refunded = 'refunded'

class Payment(Base):
    __tablename__ = 'payments'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=False)
    amount = Column(Numeric(10, 2), nullable=False)
    payment_method = Column(Enum(PaymentMethod), nullable=False)
    payment_type = Column(Enum(PaymentType), nullable=False, default=PaymentType.full)
    deposit_percentage = Column(Integer, nullable=True)
    related_payment_id = Column(Integer, ForeignKey('payments.id'), nullable=True)
    payment_status = Column(Enum(PaymentStatus), nullable=False, default=PaymentStatus.pending)
    transaction_id = Column(String(100), nullable=True)
    payment_date = Column(DateTime, nullable=True)
    notes = Column(Text, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    booking = relationship('Booking', back_populates='payments')
    related_payment = relationship('Payment', remote_side=[id], backref='related_payments')