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

class ServiceBookingStatus(str, enum.Enum):
    pending = 'pending'
    confirmed = 'confirmed'
    completed = 'completed'
    cancelled = 'cancelled'

class ServiceBooking(Base):
    __tablename__ = 'service_bookings'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    booking_number = Column(String(50), unique=True, nullable=False, index=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    total_amount = Column(Numeric(10, 2), nullable=False)
    status = Column(Enum(ServiceBookingStatus), nullable=False, default=ServiceBookingStatus.pending)
    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)
    user = relationship('User', back_populates='service_bookings')
    service_items = relationship('ServiceBookingItem', back_populates='service_booking', cascade='all, delete-orphan')
    payments = relationship('ServicePayment', back_populates='service_booking', cascade='all, delete-orphan')

class ServiceBookingItem(Base):
    __tablename__ = 'service_booking_items'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    service_booking_id = Column(Integer, ForeignKey('service_bookings.id'), nullable=False)
    service_id = Column(Integer, ForeignKey('services.id'), nullable=False)
    quantity = Column(Integer, nullable=False, default=1)
    unit_price = Column(Numeric(10, 2), nullable=False)
    total_price = Column(Numeric(10, 2), nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    service_booking = relationship('ServiceBooking', back_populates='service_items')
    service = relationship('Service')

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

class ServicePaymentMethod(str, enum.Enum):
    cash = 'cash'
    stripe = 'stripe'
    bank_transfer = 'bank_transfer'

class ServicePayment(Base):
    __tablename__ = 'service_payments'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    service_booking_id = Column(Integer, ForeignKey('service_bookings.id'), nullable=False)
    amount = Column(Numeric(10, 2), nullable=False)
    payment_method = Column(Enum(ServicePaymentMethod), nullable=False)
    payment_status = Column(Enum(ServicePaymentStatus), nullable=False, default=ServicePaymentStatus.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)
    service_booking = relationship('ServiceBooking', back_populates='payments')