from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
import re
# from finance_and_accounting.models import Deposit
# from finance_and_accounting.models import ChartOfAccount
from human_resource.models import StaffPayrollInstance, StaffProfile

from megawatt_agents.models import AgentProfile
from procurement.models import ProductPurchaseInstance
from system_administration.models import CompanyBranch, CompanyProfile
from warehouse_management.models import Inventory, Product, Warehouse
# kindly note to access model instances from the top using related names
# products


class ProductImageCatalogue(models.Model):
    product = models.ForeignKey(
        Product, null=True, on_delete=models.CASCADE, related_name="product_images")
    product_image = models.ImageField(blank=True)
    image_attribute = models.CharField(max_length=50, default="", blank=True)
    use_as_main_image = models.BooleanField(default=False)

    class Meta:
        # ordering= ("first_name", "last_name",)
        verbose_name = "Product Image Catalogue"
        verbose_name_plural = "Product Image Catalogues"


class ProductFeaturesCatalogue(models.Model):
    product = models.ForeignKey(
        Product, null=True, on_delete=models.CASCADE, related_name="product_features")
    product_feature_title = models.CharField(
        max_length=50, default="", blank=True)


class ProductFeaturesAttributes(models.Model):
    product_feature_catalogue = models.ForeignKey(
        ProductFeaturesCatalogue, null=True, on_delete=models.CASCADE, related_name="product_feature_attributes")
    feature_attribute_title = models.CharField(
        max_length=50, default="", blank=True)
    feature_attribute_description = models.CharField(
        max_length=500, default="", blank=True)


class ProductComponent(models.Model):
    product = models.ForeignKey(
        Product, null=True, on_delete=models.CASCADE, related_name="product_components")
    component_name = models.CharField(
        max_length=50, default="", blank=True)
    component_quantity = models.CharField(
        max_length=12, default="1", blank=True)

# brands will be stored and the sales team will be able to select from brands or add new


class ProductBrand(models.Model):
    product = models.ForeignKey(
        Product, null=True, on_delete=models.SET_NULL, related_name="product_brands")
    product_brand_name = models.CharField(
        max_length=50, default="", blank=True)
    product_brand_description = models.CharField(
        max_length=500, default="", blank=True)


class ProductPricing(models.Model):
    product = models.OneToOneField(
        Product, null=True, on_delete=models.CASCADE, related_name="product_pricing")
    product_net_price = models.CharField(
        max_length=50, default="0.00",)
    product_net_price_technician = models.CharField(
        max_length=50, default="0.00",)
    product_net_price_reseller = models.CharField(
        max_length=50, default="0.00",)
    product_net_price_financier = models.CharField(
        max_length=50, default="0.00",)
    product_net_price_engineering_procurement_contractor = models.CharField(
        max_length=50, default="0.00",)
    product_net_price_hydraulic_engineers = models.CharField(
        max_length=50, default="0.00",)


class ProductDiscount(models.Model):
    product = models.OneToOneField(
        Product, null=True, on_delete=models.CASCADE, related_name="product_discount")
    discount_type_choices = [
        ("percentage_discount", "Percentage Discount"), ("fixed_discount", "Fixed Discount"), ("not_selected", "Not Selected")]
    discount_type = models.CharField(
        max_length=20, choices=discount_type_choices, default="not_selected",)
    discount_value = models.CharField(
        max_length=5, default="0.00",)


class ProductVAT(models.Model):
    VAT_CATEGORY_CHOICES = [
        ("standard", "Standard Rated"),
        ("zero_rated", "Zero Rated"),
        ("exempt", "Exempt"),
    ]
    product = models.OneToOneField(
        Product, null=True, on_delete=models.CASCADE, related_name="product_vat")
    vat_percentage_value = models.CharField(
        max_length=5, default="0.00",)
    vat_category = models.CharField(
        max_length=20, choices=VAT_CATEGORY_CHOICES, default="standard")
    # vat_absolute_value = models.CharField(
    #     max_length=5, default="0.00",)

# sales


class SaleOutlet(models.Model):
    company_branch = models.ForeignKey(
        CompanyBranch, null=True, on_delete=models.SET_NULL, related_name="branch_sale_outlets")
    sale_outlet_name = models.CharField(max_length=50, default="", unique=True)
    sale_outlet_location = models.CharField(
        max_length=50, default="",)
    sale_outlet_contact_phone = models.CharField(
        max_length=25, default="", blank=False)
    sale_outlet_description = models.CharField(
        max_length=500, default="", blank=True)
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sale_outlet_created_by")
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sale_outlet_last_updated_by")
    recycle_bin = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)


class CustomerProfile(models.Model):
    user = models.OneToOneField(
        User, null=True, on_delete=models.SET_NULL, related_name="user_customer_profile")
    company_profile = models.ForeignKey(
        CompanyProfile, null=True, on_delete=models.CASCADE, related_name="company_customers")
    agent = models.ForeignKey(
        AgentProfile, null=True, on_delete=models.CASCADE, related_name="agent_managing_customer_profile")
    customer_first_name = models.CharField(
        max_length=20, default="", blank=False)
    customer_last_name = models.CharField(
        max_length=20, default="", blank=False)
    # added
    company_name = models.CharField(
        max_length=100, default="", blank=True)
    customer_type_choices = [("engineering_procurement_contractor", "Engineering Procurement Contractor"), ("technician", "Technician"), ("reseller", "Reseller"),
                             ("financier", "Financier"), ("end_user", "End User"), ("hydraulic_engineers", "Hydraulic Engineers"), ("not_selected", "Not Selected")]
    customer_type = models.CharField(
        max_length=50, choices=customer_type_choices, default="not_selected", blank=False)
    # end added
    email_address = models.EmailField(max_length=50, default="", blank=False)
    phone_number = models.CharField(max_length=25, default="", blank=False)
    title_choices = [("mr", "Mr."), ("mrs", "Mrs."), ("miss", "Miss"),
                     ("other", "Other"), ("not_selected", "Not Selected")]
    customer_title = models.CharField(
        max_length=20, choices=title_choices, default="not_selected", blank=False)
    is_profile_set = models.BooleanField(default=False)
    recycle_bin = models.BooleanField(default=False)
    # email_verification_code = models.CharField(max_length=6,null=True,blank=True)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    kra_pin = models.CharField(
        max_length=20, default="", blank=True)
    certificate_of_registration_number = models.CharField(
        max_length=25, default="", blank=True)
    customer_credit_limit = models.CharField(
        max_length=50, default="0.00", blank=True)
    customer_payment_terms_days = models.CharField(
        max_length=10, default="30", blank=True)
    # Verification fields (mirrored from megawatt Laravel)
    is_verified = models.BooleanField(default=False)
    verification_requested_at = models.DateTimeField(null=True, blank=True)
    verified_at = models.DateTimeField(null=True, blank=True)
    verified_by = models.ForeignKey(
        'human_resource.StaffProfile', null=True, blank=True,
        on_delete=models.SET_NULL, related_name="verified_customers")
    review_comment = models.TextField(default="", blank=True)

    class Meta:
        verbose_name = "Customer Profile"
        verbose_name_plural = "Customer Profiles"


class CustomerDocs(models.Model):
    customer_profile = models.ForeignKey(
        CustomerProfile, null=True, on_delete=models.CASCADE, related_name="customer_documents")
    type_choices = [
        ("pin_certificate", "KRA PIN Certificate"),
        ("certificate_of_registration", "Certificate of Registration"),
        ("solar_installation_certificate", "Solar Installation Certificate"),
        ("single_business_permit", "Single Business Permit"),
        ("id_copy", "ID Copy"),
        ("not_selected", "Not Selected"),
    ]
    document_type = models.CharField(
        max_length=100, choices=type_choices, default="not_selected", blank=False)
    saved_document_path = models.CharField(
        max_length=100, default="not_selected", blank=True)


class CustomerAccountManager(models.Model):
    """Links a customer to the staff member managing their account.

    Ported from the live ERP `marketing` app so the CRM customer-registration
    flow (account-manager assignment) works end-to-end in this structure.
    """
    staff_profile = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="staff_customer_accounts")
    customer_profile = models.OneToOneField(
        CustomerProfile, null=True, on_delete=models.CASCADE, related_name="account_manager")
    customer_account_manager_description = models.CharField(
        max_length=500, default="", blank=True)

    def __str__(self):
        return f"{self.customer_profile} → {self.staff_profile}"


class SalesQuotation(models.Model):
    company_branch = models.ForeignKey(
        CompanyBranch, null=True, on_delete=models.SET_NULL, related_name="branch_sale_quotations")
    sales_quotation_number = models.CharField(
        max_length=50, unique=True, default="")
    customer_profile = models.ForeignKey(
        CustomerProfile, null=True, on_delete=models.CASCADE, related_name="customer_sale_quotations")
    quotation_net_value = models.CharField(
        max_length=50, default="0.00",)
    sales_quotation_approved = models.BooleanField(
        default=False)  # sale quotation accepted
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_quotation_created_by")
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_quotation_last_updated_by")
    recycle_bin = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(default=timezone.now)
    quotation_price_change_request_active = models.BooleanField(default=False)
    crm_quote_uuid = models.CharField(max_length=36, blank=True, default="")
    synced_to_crm = models.BooleanField(default=False)
    ORIGIN_CHOICES = [("erp", "ERP"), ("crm", "CRM")]
    origin = models.CharField(max_length=10, choices=ORIGIN_CHOICES, default="erp")

    def save(self, *args, **kwargs):
        if not self.pk:
            numberReg = re.sub(
                r'[^0-9]', '', self.timestamp.strftime('%Y-%m-%d %H:%M:%S'))
            self.sales_quotation_number = "SQ"+numberReg
        super().save(*args, **kwargs)


class SalesQuotationItem(models.Model):
    sales_quotation = models.ForeignKey(
        SalesQuotation, null=True, on_delete=models.CASCADE, related_name="sales_quotation_items")
    product = models.ForeignKey(
        Product, null=True, on_delete=models.CASCADE, related_name="sales_quotation_products")
    quantity = models.CharField(max_length=15, default="0",)
    price_per_item = models.CharField(
        max_length=50, default="0.00",)
    request_price_change = models.BooleanField(default=False)
    requested_price_change_approved = models.BooleanField(default=False)
    requested_new_price_after_discount = models.CharField(
        max_length=50, default="0.00",)
    discount_per_item = models.CharField(
        max_length=50, default="0.00",)
    gross_subtotal = models.CharField(
        max_length=50, default="0.00",)
    total_discount = models.CharField(
        max_length=50, default="0.00",)
    net_subtotal = models.CharField(
        max_length=50, default="0.00",)


# class SalesInventory(models.Model):
#     product = models.OneToOneField(
#         Product, null=True, on_delete=models.SET_NULL, related_name="product_sales_inventories")
#     sale_outlet = models.ForeignKey(
#         SaleOutlet, null=True, on_delete=models.CASCADE, related_name="sale_outlet_sales_inventory")
#     quantity = models.CharField(max_length=15, default="0",)
#     minimum_stock_level = models.CharField(max_length=15, default="0",)
#     sales_inventory_description = models.CharField(
#         max_length=500, default="",)
#     created_by = models.ForeignKey(
#         StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_inventory_created_by")
#     last_updated_by = models.ForeignKey(
#         StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_inventory_last_updated_by")
#     recycle_bin = models.BooleanField(default=False)
#     created_on = models.DateTimeField(auto_now_add=True)
#     last_updated_on = models.DateTimeField(auto_now=True)

#     class Meta:
#         # ordering= ("first_name", "last_name",)
#         verbose_name = "Sales Inventory"
#         verbose_name_plural = "Sales Inventories"


class CustomerShippingAddress(models.Model):
    customer_profile = models.ForeignKey(
        CustomerProfile, null=True, on_delete=models.CASCADE, related_name="customer_shipping_address")
    shipping_address = models.CharField(
        max_length=100, default="", blank=False)
    pick_up_point = models.CharField(
        max_length=100, default="", blank=False)
    shipping_town = models.CharField(
        max_length=50, default="", blank=False)


# customers orders created online will be assigned to the main branch
class CustomerOrder(models.Model):
    company_branch = models.ForeignKey(
        CompanyBranch, null=True, on_delete=models.SET_NULL, related_name="branch_customer_orders")
    customer_order_number = models.CharField(
        max_length=50, unique=True, default="")
    customer_profile = models.ForeignKey(
        CustomerProfile, null=True, on_delete=models.SET_NULL, related_name="customer_order_history")
    source_quotation = models.ForeignKey(
        "SalesQuotation", null=True, blank=True, on_delete=models.SET_NULL,
        related_name="converted_customer_orders")
    agent_profile = models.ForeignKey(
        AgentProfile, null=True, on_delete=models.SET_NULL, related_name="agent_order_history")  # add functionalities to match the agents module
    order_type_choices = [("order_online", "Order Online"),
                          ("order_offline", "Order Offline"), ("not_selected", "Not Selected")]
    customer_order_type = models.CharField(
        max_length=20, choices=order_type_choices, default="not_selected", blank=False)
    customer_order_description = models.CharField(
        max_length=500, default="", blank=True)  # edited by the sales team, stores agent quotation number for sales made by agents
    customer_order_total_gross_value = models.CharField(
        max_length=50, default="0.00",)
    customer_order_total_discount = models.CharField(
        max_length=50, default="0.00",)
    customer_order_total_net_value = models.CharField(
        max_length=50, default="0.00",)
    customer_order_total_amount_paid = models.CharField(
        max_length=50, default="0.00",)
    customer_order_approved = models.BooleanField(
        default=False)  # true when it has been paid or okayed to be paid by the finance
    customer_order_fulfilled_for_transit = models.BooleanField(default=False)
    customer_ordered_delivered_to_destination = models.BooleanField(
        default=False)
    customer_order_picked_by_customer = models.BooleanField(default=False)
    # customer must first cancel the order to return the items
    customer_order_cancelled_by_customer = models.BooleanField(default=False)
    customer_order_cancelled_by_sales_team = models.BooleanField(
        default=False)  # in the event that the order is unable to be fullfilled
    sales_person = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="customer_offline_sales_representative")  # represents the sles person that made the sales possible for commissions purposes
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="customer_offline_order_created_by")  # used for offline orders to capture the sales team that serves the order
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="customer_offline_order_last_updated_by")  # used for offline orders to capture the sales team that serves the order
    recycle_bin = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    expected_delivery_date = models.DateField()
    timestamp = models.DateTimeField(default=timezone.now)
    ORIGIN_CHOICES = [("erp", "ERP"), ("crm", "CRM")]
    origin = models.CharField(max_length=10, choices=ORIGIN_CHOICES, default="erp")

    def save(self, *args, **kwargs):
        if not self.pk:
            numberReg = re.sub(
                r'[^0-9]', '', self.timestamp.strftime('%Y-%m-%d %H:%M:%S'))
            self.customer_order_number = "CO"+numberReg
        super().save(*args, **kwargs)


class CustomerOrderItem(models.Model):
    customer_order = models.ForeignKey(
        CustomerOrder, null=True, on_delete=models.CASCADE, related_name="customer_order_items")
    product = models.ForeignKey(
        Product, null=True, on_delete=models.CASCADE, related_name="customer_order_products")
    quantity = models.CharField(max_length=15, default="0",)
    price_per_item = models.CharField(
        max_length=50, default="0.00",)
    discount_per_item = models.CharField(
        max_length=50, default="0.00",)
    gross_subtotal = models.CharField(
        max_length=50, default="0.00",)
    total_discount = models.CharField(
        max_length=50, default="0.00",)
    net_subtotal = models.CharField(
        max_length=50, default="0.00",)
    sales_item_order_fulfilled = models.BooleanField(default=False)


class CustomerOrderDocument(models.Model):
    """Documents attached to a customer order (LPO, Proof of Payment).

    Pushed from the CRM via the API (base64) when a quote is converted to an
    order, so finance can review the POP/LPO during approval.
    """
    DOCUMENT_TYPES = [
        ("lpo", "Local Purchase Order"),
        ("pop", "Proof of Payment"),
        ("other", "Other"),
    ]
    customer_order = models.ForeignKey(
        CustomerOrder, null=True, on_delete=models.CASCADE, related_name="order_documents")
    document_type = models.CharField(
        max_length=20, choices=DOCUMENT_TYPES, default="other")
    original_name = models.CharField(max_length=255, default="", blank=True)
    saved_document_path = models.CharField(max_length=500, default="", blank=True)
    uploaded_via = models.CharField(max_length=20, default="crm", blank=True)
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-id"]

    def __str__(self):
        return f"{self.customer_order} — {self.document_type}"


class SalesOrder(models.Model):
    customer_order = models.OneToOneField(
        CustomerOrder, null=True, on_delete=models.CASCADE, related_name="sales_order_customer_order")
    sales_order_number = models.CharField(
        max_length=50, unique=True, default="")
    sale_outlet = models.ForeignKey(
        SaleOutlet, null=True, on_delete=models.CASCADE, related_name="sale_outlet_sales_order")
    warehouse = models.ForeignKey(
        Warehouse, null=True, on_delete=models.SET_NULL, related_name="sales_order_warehouse")
    sales_order_description = models.CharField(
        max_length=500, default="", blank=True)
    sales_order_approved = models.BooleanField(default=False)
    sales_order_fulfilled = models.BooleanField(default=False)
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_order_created_by")
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_order_last_updated_by")
    recycle_bin = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(default=timezone.now)

    def save(self, *args, **kwargs):
        if not self.pk:
            numberReg = re.sub(
                r'[^0-9]', '', self.timestamp.strftime('%Y-%m-%d %H:%M:%S'))
            self.sales_order_number = "SO"+numberReg
        super().save(*args, **kwargs)

# if you delete a sales order items, the quantity must be returned to the respective inventory


class SalesOrderItems(models.Model):
    sales_order = models.ForeignKey(
        SalesOrder, null=True, on_delete=models.CASCADE, related_name="sales_order_sales_items")
    product = models.ForeignKey(
        Product, null=True, on_delete=models.CASCADE, related_name="product_sales_order_items")
    quantity = models.CharField(max_length=15, default="0",)
    sales_item_order_fulfilled = models.BooleanField(default=False)


class SalesCommission(models.Model):
    product = models.ManyToManyField(
        Product, blank=True, related_name="product_sales_commissions")
    commission_title = models.CharField(
        max_length=50, default="", blank=False)
    commission_description = models.CharField(
        max_length=500, default="",)
    commission_value = models.CharField(max_length=15, default="0",)
    sales_person = models.ManyToManyField(
        StaffProfile, blank=True, related_name="sales_commission_sales_persons")
    agent = models.ManyToManyField(
        AgentProfile, blank=True, related_name="sales_commission_agents")
    is_margin_based = models.BooleanField(default=False)  # if margin based
    # is_test_data = models.BooleanField(default=False)  # if margin based
    commission_period_start_date = models.DateField()
    commission_period_end_date = models.DateField()
    commission_module_choices = [
        ("percentage", "Percentage"), ("fixed", "Fixed"), ("not_selected", "Not Selected")]
    commission_module = models.CharField(
        max_length=20, choices=commission_module_choices, default="not_selected", blank=False)
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_commission_created_by")  # used for offline orders to capture the sales team that serves the order
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="sales_commission_last_updated_by")  # used for offline orders to capture the sales team that serves the order
    recycle_bin = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)

# commision sheet, will be prepared by the head of sales


class CommissionSheet(models.Model):
    company_branch = models.ForeignKey(
        CompanyBranch, null=True, on_delete=models.SET_NULL, related_name="branch_commission_sheets")
    commission_sheet_title = models.CharField(
        max_length=100, default="", blank=False)
    commission_sheet_number = models.CharField(max_length=50, unique=True)
    commission_sheet_description = models.CharField(
        max_length=500, default="", blank=False)
    month_titles_choices = [
        ("january", "January"), ("february", "February"), ("march", "March"), ("april", "April"), ("may", "May"), ("june", "June"), ("july", "July"), ("august", "August"), ("september", "September"), ("october", "October"), ("november", "November"), ("december", "December"), ("not_selected", "Not Selected")]
    commission_sheet_for_the_month_of = models.CharField(
        max_length=100, choices=month_titles_choices, default="not_selected", blank=False)
    commission_sheet_for_the_year = models.CharField(
        max_length=15, default="2024", blank=False)
    commission_sheet_value = models.CharField(
        max_length=15, default="0.00", blank=False)
    commission_sheet_approved_by_finance = models.BooleanField(default=False)
    commission_sheet_payment_settled = models.BooleanField(default=False)
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="commission_sheets_created_by")
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="commission_sheets_last_updated_by")
    recycle_bin = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(default=timezone.now)

    def save(self, *args, **kwargs):
        if not self.pk:
            numberReg = re.sub(
                r'[^0-9]', '', self.timestamp.strftime('%Y-%m-%d %H:%M:%S'))
            self.commission_sheet_number = "COM"+numberReg
        super().save(*args, **kwargs)


class CommissionSheetInstance(models.Model):
    # staff_profile = models.ForeignKey(
    #     StaffProfile, null=True, on_delete=models.SET_NULL,related_name="staff_commission_sheets")
    staff_payroll_instance = models.ForeignKey(StaffPayrollInstance, null=True,
                                               on_delete=models.SET_NULL, related_name="staff_payroll_instance_commissions")
    agent_profile = models.ForeignKey(AgentProfile, null=True,
                                      on_delete=models.SET_NULL, related_name="agent_profile_commision_payment")
    customer_order = models.ForeignKey(
        CustomerOrder, null=True, on_delete=models.CASCADE, related_name="customer_order_commissions")
    commission_sheet = models.ForeignKey(
        CommissionSheet, null=True, on_delete=models.SET_NULL, related_name="commission_items")
    commission_value = models.CharField(
        max_length=12, default="0.00", blank=False)
    # created_by = models.ForeignKey(
    #     StaffProfile, null=True, on_delete=models.SET_NULL, related_name="commission_items_created_by")
    # last_updated_by = models.ForeignKey(
    #     StaffProfile, null=True, on_delete=models.SET_NULL, related_name="commission_items_last_updated_by")
    # recycle_bin = models.BooleanField(default=False)
    # created_on = models.DateTimeField(auto_now_add=True)
    # last_updated_on = models.DateTimeField(auto_now=True)


class OrderItemReturn(models.Model):
    customer_order = models.ForeignKey(
        CustomerOrder, null=True, on_delete=models.CASCADE, related_name="customer_order_items_return")
    recycle_bin = models.BooleanField(default=False)
    refund_requested = models.BooleanField(default=False)
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    date_of_return = models.DateField()


class ReturnItem(models.Model):
    order_item_return = models.ForeignKey(
        OrderItemReturn, null=True, on_delete=models.CASCADE, related_name="order_items_return")
    ordered_item_returned = models.OneToOneField(
        CustomerOrderItem, null=True, on_delete=models.CASCADE, related_name="ordered_items_returned")
    reason_for_return = models.CharField(
        max_length=500, default="", blank=False)


class LandedCost(models.Model):
    company_profile = models.ForeignKey(
        CompanyProfile, null=True, on_delete=models.CASCADE, related_name="company_landed_costs")
    name_of_expense = models.CharField(
        max_length=50, default="", blank=True)
    description_of_calculation = models.CharField(
        max_length=50, default="", blank=True)
    created_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="landed_costs_created_by_staff")
    last_updated_by = models.ForeignKey(
        StaffProfile, null=True, on_delete=models.SET_NULL, related_name="landed_costs_last_updated_by_staff")
    created_on = models.DateTimeField(auto_now_add=True)
    last_updated_on = models.DateTimeField(auto_now=True)
    recycle_bin = models.BooleanField(default=False)


class LandedCostInstance(models.Model):
    landed_cost = models.ForeignKey(
        LandedCost, null=True, on_delete=models.CASCADE, related_name="landed_cost_instances")
    product_purchase_instance = models.ForeignKey(
        ProductPurchaseInstance, null=True, on_delete=models.CASCADE, related_name="landed_cost_product_purchase_instances")  # GOING TO BE ONE TO ONE
    cost_value = models.CharField(max_length=12, blank=True, default="0.00")
    currency_choices = [("usd", "USD"), ("gbp", "GBP"), ("eur", "EUR"),
                        ("kes", "KES"), ("not_selected", "Not Selected")]
    cost_currency = models.CharField(
        max_length=30, choices=currency_choices, default="kes")
    exchange_rate = models.CharField(
        max_length=12, blank=True, default="1.00")
    cost_amount_paid = models.CharField(
        max_length=12, blank=True, default="0.00")
    cost_amount_due = models.CharField(
        max_length=12, blank=True, default="0.00")
    cost_payment_settled = models.BooleanField(default=False)
    reference = models.CharField(
        max_length=50, default="")
