import datetime
from typing import List

from pydantic import BaseModel, Field

from .models import ContractDraft, DraftProvision, DraftParty, Contract

from ..entity.models import Entity
from ..core.schemas import Writer
from ..core.models import DictionaryEntry


class ContractDraftRead(ContractDraft):
    pass


class ContractDraftCreate(Writer):
    name: str = Field(title='Nom')
    title: str = Field(title='Titre')
    parties: List[DraftParty] = Field(title='Parties')
    provisions: List[DraftProvision] = Field(
        props={"items-per-row": "1", "numbered": True},
        title='Clauses'
    )
    variables: List[DictionaryEntry] = Field(
        default=[],
        format="dictionary",
        title='Variables'
    )

    async def validate_foreign_key(self):
        return
        for p in self.parties:
            p.entity = await Entity.get(p.entity)
            if p.entity is None:
                raise ValueError


class ContractDraftUpdate(ContractDraftCreate):
    pass


class ForeignEntityRead(BaseModel):
    label: str

    class Config:
        title = "Avocat"


class PartyRead(BaseModel):
    signature_affixed: bool = Field(title='Signature apposée?')
    signature_uuid: str = Field(format="signature-link", title="Lien vers signature")
    part: str = Field(title='Rôle')
    entity: ForeignEntityRead = Field(title='Client')

    class Config:
        title = "Partie"


class ContractRead(Contract):
    parties: List[PartyRead]
    lawyer: ForeignEntityRead

    class Config:
        title = "Contrat"


class ContractCreate(Writer):
    date: datetime.date
    location: str
    draft_id: str


class ContractUpdate(BaseModel):
    pass


class SignatureRead(BaseModel):
    signature_uuid: str
    entity: Entity
    part: str
    representative: Entity = None
    signature_affixed: bool
    contract_label: str
