from django.db import models
from django.db.models.deletion import CASCADE
from django.db.models.fields import AutoField, CharField, DateField
from django_resized import ResizedImageField
from accounts.models import Users
from sub_categories.models import Sub_Categories
from works.models import Works
# Create your models here.


class Work_Contract(models.Model):

    STATUS_CHOICES = (
        (0, 'Pending'),
        (1, 'Accepted'),
        (2, 'Rejected'),
        (3, 'In Progress'),
        (4, 'Completed'),
        (5, 'Cancelled'),
    )

    PAYMENT_STATUS = (
        (0, 'Pending'),
        (1, 'Partial'),
        (2, 'Paid'),
    )

    CONTRACT_TYPE = (
        (0, 'Non Escrow'),
        (1, 'Escrow'),
    )

    ESCROW_STATUS = (
        (0, 'Not Applicable'),
        (1, 'Pending Deposit'),
        (2, 'Deposited'),
        (3, 'Released'),
        (4, 'Refunded'),
    )

    FLOW_STATUS = (
        (0, 'Draft'),
        (1, 'Sent'),
        (2, 'Modified'),
        (3, 'Accepted'),
        (4, 'Rejected'),
    )

    work = models.ForeignKey(
        Works,
        on_delete=models.CASCADE,
        related_name='contracts'
    )

    sub_category = models.ForeignKey(
        Sub_Categories,
        default="",
        blank=False,
        null=False,
        on_delete=models.CASCADE,
        related_name="contract_sub_category"
    )

    created_by = models.ForeignKey(
        Users,
        default="",
        on_delete=models.CASCADE,
        related_name='contracts_created'
    )

    created_for = models.ForeignKey(
        Users,
        default="",
        on_delete=models.CASCADE,
        related_name='contracts_received'
    )


    last_modified_by = models.ForeignKey(
        Users,
        on_delete=models.CASCADE,
        related_name='modified_contracts',
        blank=True,
        null=True
    )

    pending_with = models.ForeignKey(
        Users,
        on_delete=models.CASCADE,
        related_name='pending_contracts',
        blank=True,
        null=True
    )

    title = models.CharField(
        max_length=255
    )

    description = models.TextField(
        blank=True,
        null=True
    )

    amount = models.DecimalField(
        max_digits=12,
        decimal_places=2,
        default=0
    )

    contract_type = models.IntegerField(
        choices=CONTRACT_TYPE,
        default=0
    )

    escrow_amount = models.DecimalField(
        max_digits=12,
        decimal_places=2,
        default=0
    )

    escrow_status = models.IntegerField(
        choices=ESCROW_STATUS,
        default=0
    )

    escrow_released_at = models.DateTimeField(
        blank=True,
        null=True
    )

    start_date = models.DateField(
        blank=True,
        null=True
    )

    end_date = models.DateField(
        blank=True,
        null=True
    )

    status = models.IntegerField(
        choices=STATUS_CHOICES,
        default=0
    )

    flow_status = models.IntegerField(
        choices=FLOW_STATUS,
        default=0
    )

    payment_status = models.IntegerField(
        choices=PAYMENT_STATUS,
        default=0
    )

    customer_approved = models.BooleanField(
        default=False
    )

    contractor_approved = models.BooleanField(
        default=False
    )

    terms_conditions = models.TextField(
        blank=True,
        null=True
    )

    message = models.TextField(
        blank=True,
        null=True
    )

    accepted_at = models.DateTimeField(
        blank=True,
        null=True
    )

    created_at = models.DateTimeField(
        auto_now_add=True
    )

    updated_at = models.DateTimeField(
        auto_now=True
    )