import * as database from "../database/index.js";
import { business } from "../index.js";

/**
 * Represents a paragraph or section in a contract.
 */
export type TContractParagraph = {
  id: string;
  title?: string;
  content: string;  // Markdown formatted text
  order: number;
  type: "heading" | "clause" | "subclause" | "definition" | "exhibit";
  isRequired: boolean;
  metadata?: {
    applicableJurisdictions?: string[];
    tags?: string[];
    lastModified?: number;
    versionId?: string;
  };
};

/**
 * Contract party with signature information.
 */
export type TContractParty = {
  signingOrder: number;
  referencedAs: string;
  person: business.TPerson;
  role: "signer" | "cc" | "witness";
  signature: {
    given: boolean;
    timestamp?: number;
    location?: string;
    ip?: string;
    verifications?: any[];
  };
};

/**
 * Contract attachment like exhibits, appendices.
 */
export type TContractAttachment = {
  id: string;
  title: string;
  type: "exhibit" | "appendix" | "schedule";
  fileReference?: string;
  content?: string;  // Markdown or reference
};

/**
 * Base envelope type for all contract types, extending the common document type.
 */
export type TContractEnvelope<TYPE extends string, FIELDS> = business.TDocumentEnvelope<
  TYPE,
  {
    // Contract-specific dates
    effectiveDate: number;
    expirationDate?: number;
    
    // Contract-specific status
    status: "draft" | "negotiation" | "active" | "expired" | "terminated" | "renewed";
    
    // Parties (multiple, without sender/recipient distinction)
    parties: TContractParty[];
    
    // Structured content
    paragraphs: TContractParagraph[];
    
    // Additional attachments
    attachments?: TContractAttachment[];
  } & FIELDS
>;

/**
 * Employment contract specific type.
 */
export type TEmploymentContract = TContractEnvelope<
  "employment", 
  {
    employmentTerms: {
      startDate: number;
      position: string;
      compensationDetails: string;
      workingHours?: string;
      location?: string;
      probationPeriod?: {
        durationInMonths: number;
        terms?: string;
      };
    };
  }
>;

/**
 * Non-disclosure agreement specific type.
 */
export type TNDAContract = TContractEnvelope<
  "nda", 
  {
    confidentialityTerms: {
      duration: number;  // In months
      scope: string;
      exclusions?: string[];
    };
  }
>;

/**
 * Service agreement specific type.
 */
export type TServiceContract = TContractEnvelope<
  "service", 
  {
    serviceTerms: {
      scope: string;
      deliverables: string[];
      timeline?: {
        milestones: {
          description: string;
          dueDate: number;
        }[];
      };
      paymentTerms: string;
    };
  }
>;

/**
 * Real estate lease agreement specific type.
 */
export type TLeaseContract = TContractEnvelope<
  "lease", 
  {
    propertyDetails: {
      address: business.IAddress;
      propertyType: string;
      areaSize?: number;
      areaSizeUnit?: string;
    };
    leaseTerms: {
      rentAmount: number;
      rentCurrency: string;
      paymentFrequency: "monthly" | "quarterly" | "annually";
      depositAmount?: number;
      utilities?: string[];
    };
  }
>;

/**
 * Union type for all contract types.
 */
export type TContract = 
  | TEmploymentContract 
  | TNDAContract 
  | TServiceContract 
  | TLeaseContract;
