from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Float, Text
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
import uuid
import hashlib
from ..database import Base


def generate_share_code():
    """Generate a unique 10-character share code"""
    unique_id = uuid.uuid4().hex
    return hashlib.sha256(unique_id.encode()).hexdigest()[:10].upper()


class VehicleShare(Base):
    """Track vehicle shares with price markup"""
    __tablename__ = "vehicle_shares"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)  # User who shared

    # Reference to the original vehicle
    request_vehicle_id = Column(Integer, ForeignKey("request_vehicles.id"), nullable=False)

    # Share code for the link
    share_code = Column(String(10), unique=True, index=True, nullable=False)

    # Pricing
    original_price_krw = Column(Float, nullable=False)  # Original vehicle price
    markup_amount_krw = Column(Float, default=0)  # Additional amount added by sharer
    shared_price_krw = Column(Float, nullable=False)  # Total shared price (original + markup)

    # Statistics
    view_count = Column(Integer, default=0)
    is_purchased = Column(Boolean, default=False)
    purchased_by_user_id = Column(Integer, ForeignKey("users.id"), nullable=True)

    # Timestamps
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    expires_at = Column(DateTime(timezone=True), nullable=True)  # Optional expiration
    purchased_at = Column(DateTime(timezone=True), nullable=True)

    # Relationships
    user = relationship("User", foreign_keys=[user_id], backref="vehicle_shares")
    purchased_by = relationship("User", foreign_keys=[purchased_by_user_id])
    request_vehicle = relationship("RequestVehicle", backref="shares")


class ShareReward(Base):
    """Track rewards earned from vehicle shares"""
    __tablename__ = "share_rewards"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)  # User who earned the reward
    vehicle_share_id = Column(Integer, ForeignKey("vehicle_shares.id"), nullable=False)

    # Amounts
    markup_amount = Column(Float, nullable=False)  # Original markup amount
    reward_amount = Column(Float, nullable=False)  # 90% of markup
    tax_amount = Column(Float, nullable=False)  # 3.3% tax withholding
    net_amount = Column(Float, nullable=False)  # Final amount after tax

    # Status
    status = Column(String(20), default="pending")  # pending, approved, withdrawn

    # Withdrawal tracking
    withdrawal_request_id = Column(Integer, ForeignKey("withdrawal_requests.id"), nullable=True)
    withdrawn_at = Column(DateTime(timezone=True), nullable=True)

    # Timestamps
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    user = relationship("User", backref="share_rewards")
    vehicle_share = relationship("VehicleShare", backref="reward")
