# plugins/exchange/forms.py

from flask_wtf import FlaskForm
from wtforms import (
    StringField, SelectField, TextAreaField, DecimalField,
    BooleanField, DateTimeField, IntegerField, FieldList,
    FormField
)
from wtforms.validators import DataRequired, Optional, NumberRange

# ---- Listing Form ----

class ListingForm(FlaskForm):
    plant_id = SelectField(
        'Plant',
        coerce=int,
        validators=[DataRequired()]
    )
    owner_actor_type = SelectField(
        'Owner Type',
        choices=[('user', 'User'), ('vendor', 'Vendor'), ('group', 'Group')],
        validators=[DataRequired()]
    )
    owner_actor_id = SelectField(
        'Owner',
        coerce=int,
        validators=[DataRequired()]
    )
    type = SelectField(
        'Listing Type',
        choices=[
            ('sale','Sale'),
            ('trade','Trade'),
            ('auction','Auction'),
            ('loan','Loan'),
            ('donation','Donation'),
            ('request','Request'),
            ('consignment','Consignment'),
            ('swap','Swap')
        ],
        validators=[DataRequired()]
    )

    # Sale / Fixed price / BIN
    bin_price = DecimalField('Buy It Now Price', validators=[Optional(), NumberRange(min=0)])
    allow_offers = BooleanField('Allow Offers')

    # Auction
    auction_start = DecimalField('Auction Starting Bid', validators=[Optional(), NumberRange(min=0)])
    auction_reserve = DecimalField('Auction Reserve (Min Sale Price)', validators=[Optional(), NumberRange(min=0)])
    auction_ends_at = DateTimeField('Auction Ends At', format='%Y-%m-%d %H:%M', validators=[Optional()])

    # Trade
    trade_items = TextAreaField(
        'Trade Items (JSON list of plant IDs and notes)',
        validators=[Optional()]
    )

    # Loan
    loan_return_date = DateTimeField('Expected Return Date', format='%Y-%m-%d', validators=[Optional()])
    rental_rate = DecimalField('Rental Rate', validators=[Optional(), NumberRange(min=0)])

    # Consignment
    consignment_split = DecimalField('Consignment Fee (e.g., 0.10 for 10%)', validators=[Optional(), NumberRange(min=0, max=1)])

    # Swap
    swap_batch_id = SelectField('Swap Batch', coerce=int, validators=[Optional()])

    status = SelectField(
        'Status',
        choices=[('active','Active'), ('inactive','Inactive')],
        default='active',
        validators=[DataRequired()]
    )


# ---- Offer Form ----

class OfferForm(FlaskForm):
    amount = DecimalField('Offer Amount', validators=[DataRequired(), NumberRange(min=0)])
    notes = TextAreaField('Notes', validators=[Optional()])


# ---- Ownership Snapshot Form ----

class OwnershipForm(FlaskForm):
    listing_id = SelectField('Listing', coerce=int, validators=[DataRequired()])
    acquired_on = DateTimeField('Acquired On', format='%Y-%m-%d', validators=[DataRequired()])
    price_paid = DecimalField('Price Paid', validators=[DataRequired(), NumberRange(min=0)])
    source_notes = TextAreaField('Source Notes', validators=[Optional()])


# ---- Transfer / Loan Form ----

class TransferForm(FlaskForm):
    listing_id = SelectField('Listing', coerce=int, validators=[DataRequired()])
    to_actor_type = SelectField(
        'Transfer To',
        choices=[('user','User'), ('vendor','Vendor'), ('group','Group')],
        validators=[DataRequired()]
    )
    to_actor_id = SelectField('Recipient', coerce=int, validators=[DataRequired()])
    is_loan = BooleanField('This is a loan (expect return)')
    notes = TextAreaField('Notes', validators=[Optional()])


class TransferApproveForm(FlaskForm):
    status = SelectField(
        'Decision',
        choices=[('approved','Approve'), ('rejected','Reject'), ('returned','Mark Returned')],
        validators=[DataRequired()]
    )
    notes = TextAreaField('Notes', validators=[Optional()])


# ---- Price Submission Form ----

class SubmissionForm(FlaskForm):
    taxon_id = SelectField('Taxon', coerce=int, validators=[DataRequired()])
    listing_id = SelectField('Listing (if applicable)', coerce=int, validators=[Optional()])
    source_platform = StringField('Platform (e.g. Facebook, Home Depot)', validators=[DataRequired()])
    source_vendor_name = StringField('Vendor Name (if known)', validators=[Optional()])
    original_price = DecimalField('Original Price (if known)', validators=[Optional(), NumberRange(min=0)])
    paid_price = DecimalField('Paid Price', validators=[DataRequired(), NumberRange(min=0)])
    shipping_price = DecimalField('Shipping Price', validators=[Optional(), NumberRange(min=0)])
    tax_amount = DecimalField('Tax Amount', validators=[Optional(), NumberRange(min=0)])
    notes = TextAreaField('Additional Notes', validators=[Optional()])


# ---- Swap Batch Form ----

class SwapBatchForm(FlaskForm):
    name = StringField('Batch Name', validators=[DataRequired()])
    description = TextAreaField('Description', validators=[Optional()])
    date = DateTimeField('Event Date', format='%Y-%m-%d %H:%M', validators=[DataRequired()])
