from datetime import datetime

from sqlalchemy import (
    Column,
    ForeignKey,
    Integer,
    String,
    BigInteger,
    Numeric,
    DateTime,
)
from sqlalchemy.orm import relationship

from cntr.database import Base
from cntr.models import TimeStampMixin


class Account(Base, TimeStampMixin):
    id = Column(String(100), primary_key=True, index=True)
    name = Column(String(1000), index=True)
    parent_id = Column(String(100), ForeignKey("b2b.account.id"))
    rep_1_id = Column(String(100))
    agency_1_id = Column(String(100))
    rep_1_commission_percent = Column(Numeric(10, 2))
    rep_2_id = Column(String(100))
    agency_2_id = Column(String(100))
    rep_2_commission_percent = Column(Numeric(10, 2))
    credit_limit_amount = Column(Numeric(10, 2))
    billing_line_1 = Column(String(500))
    billing_line_2 = Column(String(500))
    billing_city = Column(String(100))
    billing_state = Column(String(2))
    bliling_zip = Column(String(100))
    billing_country = Column(String(2))
    mailling_line_1 = Column(String(500))
    mailling_line_2 = Column(String(500))
    mailling_city = Column(String(100))
    mailling_state = Column(String(2))
    mailling_zip = Column(String(100))
    mailling_country = Column(String(2))
    email = Column(String(500), index=True)
    phone = Column(String(100), index=True)
    orders = relationship("Order", back_populates="account")


class Order(Base, TimeStampMixin):
    id = Column(Integer, primary_key=True, index=True)
    hub_order_id = Column(Integer, index=True)
    merchant_order_id = Column(String(50))
    merchant_id = Column(String(20))
    merchant_vendor_id = Column(String(200))
    order_date = Column(DateTime, default=datetime.utcnow)
    order_item_count = Column(Integer)
    merchandise_cost = Column(Numeric(12, 2), default=0)
    merchandise_total = Column(Numeric(12, 2), default=0)
    shipping_total = Column(Numeric(12, 2), default=0)
    tax_total = Column(Numeric(12, 2), default=0)
    order_total = Column(Numeric(12, 2), default=0)
    credit = Column(Numeric(12, 2), default=0)
    net_days_due = Column(Integer)
    discount_amount = Column(Numeric(12, 2), default=0)
    multi_source = Column(String(3))
    packing_slip_message = Column(String(400))
    vendor_notes = Column(String(400))
    vendor_warehouse_id = Column(String(20))
    sales_division = Column(String(100))
    merchant_phone_1 = Column(String(50))
    merchant_phone_2 = Column(String(50))
    merchant_phone_3 = Column(String(50))
    customer_id = Column(String(50))
    customer_payment_method = Column(String(50))
    cancel_after_date = Column(DateTime, nullable=False)
    required_ship_date = Column(DateTime, nullable=False)
    shipping_method = Column(String(50))
    ship_to_name = Column(String(100))
    ship_to_address_1 = Column(String(255))
    ship_to_address_2 = Column(String(255))
    ship_to_city = Column(String(100))
    ship_to_state = Column(String(50))
    ship_to_postal_code = Column(String(20))
    ship_to_country = Column(String(50))
    ship_to_day_phone = Column(String(50))
    ship_to_email = Column(String(255))
    bill_to_name = Column(String(100))
    bill_to_address_1 = Column(String(255))
    bill_to_address_2 = Column(String(255))
    bill_to_city = Column(String(100))
    bill_to_state = Column(String(50))
    bill_to_postal_code = Column(String(20))
    bill_to_country = Column(String(50))
    bill_to_day_phone = Column(String(50))
    bill_to_night_phone = Column(String(50))
    bill_to_email = Column(String(255))
    return_addressee = Column(String(50))
    return_address_1 = Column(String(255))
    return_address_2 = Column(String(255))
    return_city = Column(String(100))
    return_state = Column(String(50))
    return_postal_code = Column(String(20))
    return_country = Column(String(50))
    return_location_id = Column(String(50))
    freight_collect_account = Column(String(80))
    sales_agent_1 = Column(String(60))
    sales_agent_2 = Column(String(60))
    payment_method_code = Column(String(10))
    payment_method_account_number = Column(String(80))
    payment_method_auth_code = Column(String(80))
    account_id = Column(String(100), ForeignKey("b2b.account.id", ondelete="CASCADE"))
    account = relationship("Account", back_populates="orders")
    lines = relationship("OrderLine", back_populates="order")


class OrderLine(Base, TimeStampMixin):
    id = Column(Integer, primary_key=True, index=True)
    hub_order_id = Column(String(50), nullable=False, index=True)
    merchant_order_id = Column(String(50), nullable=False, index=True)
    order_line_item_number = Column(Integer)
    merchant_line_item_number = Column(Integer)
    hub_line_id = Column(BigInteger)
    merchant_product_id = Column(String(50))
    merchant_sku = Column(String(50))
    vendor_sku = Column(String(50))
    upc = Column(String(50))
    ean = Column(BigInteger)
    color_code = Column(String(20))
    size_code = Column(String(20))
    description = Column(String(255))
    description_2 = Column(String(255))
    color_size_description = Column(String(255))
    quantity = Column(BigInteger)
    uom = Column(String(50))
    weight_unit = Column(String(2))
    unit_shipping_weight = Column(Numeric(12, 2), default=0)
    unit_price = Column(Numeric(12, 2), nullable=False)
    unit_cost = Column(Numeric(12, 2), nullable=False)
    ling_shipping = Column(Numeric(12, 2), default=0)
    ling_handling = Column(Numeric(12, 2), default=0)
    line_sub_total = Column(Numeric(12, 2), default=0)
    line_tax = Column(Numeric(12, 2), default=0)
    line_item_total = Column(Numeric(12, 2), default=0)
    line_credits = Column(Numeric(12, 2), default=0)
    vendor_warehouse_id = Column(String(20))
    packing_slip_line_message = Column(String(400))
    gift_line_message = Column(String(400))
    store_name = Column(String(50))
    personalization_data = Column(String(400))
    gift_wrap_indicator = Column(String(10))
    required_ship_date = Column(DateTime)
    required_delivery_date = Column(DateTime, nullable=False)
    requested_arrival_date = Column(DateTime)
    shipper_hub_code = Column(String(20))
    permit_number = Column(BigInteger)
    shipping_method = Column(String(50))
    ship_to_id = Column(String(20))
    ship_to_name = Column(String(50))
    ship_to_address_1 = Column(String(255))
    ship_to_address_2 = Column(String(255))
    ship_to_city = Column(String(50))
    ship_to_state = Column(String(20))
    ship_to_postal_code = Column(String(20))
    ship_to_country = Column(String(50))
    ship_to_day_phone = Column(String(50))
    ship_to_email = Column(String(50))
    order_id = Column(Integer, ForeignKey("b2b.order.id", ondelete="CASCADE"))
    order = relationship("Order", back_populates="lines")
