import { z } from 'zod';

// Schema for the BPCP CIM Review Template
export const cimReviewSchema = z.object({
  dealOverview: z.object({
    targetCompanyName: z.string().describe("Target Company Name"),
    industrySector: z.string().describe("Industry/Sector"),
    geography: z.string().describe("Geography (HQ & Key Operations)"),
    dealSource: z.string().describe("Deal Source"),
    transactionType: z.string().describe("Transaction Type"),
    dateCIMReceived: z.string().describe("Date CIM Received"),
    dateReviewed: z.string().describe("Date Reviewed"),
    reviewers: z.string().describe("Reviewer(s)"),
    cimPageCount: z.string().describe("CIM Page Count"),
    statedReasonForSale: z.string().describe("Stated Reason for Sale (if provided)"),
    employeeCount: z.string().describe("Number of employees (if stated in document)"),
  }).describe("Deal Overview section"),

  businessDescription: z.object({
    coreOperationsSummary: z.string().describe("Core Operations Summary (3-5 sentences)"),
    keyProductsServices: z.string().describe("Key Products/Services & Revenue Mix (Est. % if available)"),
    uniqueValueProposition: z.string().describe("Unique Value Proposition (UVP) / Why Customers Buy"),
    customerBaseOverview: z.object({
      keyCustomerSegments: z.string().describe("Key Customer Segments/Types"),
      customerConcentrationRisk: z.string().describe("Customer Concentration Risk (Top 5 and/or Top 10 Customers as % Revenue - if stated/inferable)"),
      typicalContractLength: z.string().describe("Typical Contract Length / Recurring Revenue % (if applicable)"),
    }),
    keySupplierOverview: z.object({
      dependenceConcentrationRisk: z.string().describe("Dependence/Concentration Risk"),
    }),
  }).describe("Business Description section"),

  marketIndustryAnalysis: z.object({
    estimatedMarketSize: z.string().describe("Estimated Market Size (TAM/SAM - if provided)"),
    estimatedMarketGrowthRate: z.string().describe("Estimated Market Growth Rate (% CAGR - Historical & Projected)"),
    keyIndustryTrends: z.string().describe("Key Industry Trends & Drivers (Tailwinds/Headwinds)"),
    competitiveLandscape: z.object({
      keyCompetitors: z.string().describe("Key Competitors Identified"),
      targetMarketPosition: z.string().describe("Target's Stated Market Position/Rank"),
      basisOfCompetition: z.string().describe("Basis of Competition"),
    }),
    barriersToEntry: z.string().describe("Barriers to Entry / Competitive Moat (Stated/Inferred)"),
  }).describe("Market & Industry Analysis section"),

  financialSummary: z.object({
    financials: z.object({
      fy3: z.object({
        revenue: z.string().describe("Revenue for FY-3"),
        revenueGrowth: z.string().describe("Revenue growth % for FY-3"),
        grossProfit: z.string().describe("Gross profit for FY-3"),
        grossMargin: z.string().describe("Gross margin % for FY-3"),
        ebitda: z.string().describe("EBITDA for FY-3"),
        ebitdaMargin: z.string().describe("EBITDA margin % for FY-3")
      }),
      fy2: z.object({
        revenue: z.string().describe("Revenue for FY-2"),
        revenueGrowth: z.string().describe("Revenue growth % for FY-2"),
        grossProfit: z.string().describe("Gross profit for FY-2"),
        grossMargin: z.string().describe("Gross margin % for FY-2"),
        ebitda: z.string().describe("EBITDA for FY-2"),
        ebitdaMargin: z.string().describe("EBITDA margin % for FY-2")
      }),
      fy1: z.object({
        revenue: z.string().describe("Revenue for FY-1"),
        revenueGrowth: z.string().describe("Revenue growth % for FY-1"),
        grossProfit: z.string().describe("Gross profit for FY-1"),
        grossMargin: z.string().describe("Gross margin % for FY-1"),
        ebitda: z.string().describe("EBITDA for FY-1"),
        ebitdaMargin: z.string().describe("EBITDA margin % for FY-1")
      }),
      ltm: z.object({
        revenue: z.string().describe("Revenue for LTM"),
        revenueGrowth: z.string().describe("Revenue growth % for LTM"),
        grossProfit: z.string().describe("Gross profit for LTM"),
        grossMargin: z.string().describe("Gross margin % for LTM"),
        ebitda: z.string().describe("EBITDA for LTM"),
        ebitdaMargin: z.string().describe("EBITDA margin % for LTM")
      })
    }),
    qualityOfEarnings: z.string().describe("Quality of earnings/adjustments impression"),
    revenueGrowthDrivers: z.string().describe("Revenue growth drivers (stated)"),
    marginStabilityAnalysis: z.string().describe("Margin stability/trend analysis"),
    capitalExpenditures: z.string().describe("Capital expenditures (LTM % of revenue)"),
    workingCapitalIntensity: z.string().describe("Working capital intensity impression"),
    freeCashFlowQuality: z.string().describe("Free cash flow quality impression"),
  }).describe("Financial Summary section"),

  managementTeamOverview: z.object({
    keyLeaders: z.string().describe("Key Leaders Identified (CEO, CFO, COO, Head of Sales, etc.)"),
    managementQualityAssessment: z.string().describe("Initial Assessment of Quality/Experience (Based on Bios)"),
    postTransactionIntentions: z.string().describe("Management's Stated Post-Transaction Role/Intentions (if mentioned)"),
    organizationalStructure: z.string().describe("Organizational Structure Overview (Impression)"),
  }).describe("Management Team Overview section"),

  preliminaryInvestmentThesis: z.object({
    keyAttractions: z.string().describe("Key Attractions / Strengths (Why Invest?)"),
    potentialRisks: z.string().describe("Potential Risks / Concerns (Why Not Invest?)"),
    valueCreationLevers: z.string().describe("Initial Value Creation Levers (How PE Adds Value)"),
    alignmentWithFundStrategy: z.string().describe("Alignment with Fund Strategy"),
  }).describe("Preliminary Investment Thesis section"),

  keyQuestionsNextSteps: z.object({
    criticalQuestions: z.string().describe("Critical Questions Arising from CIM Review"),
    missingInformation: z.string().describe("Key Missing Information / Areas for Diligence Focus"),
    preliminaryRecommendation: z.string().describe("Preliminary Recommendation"),
    rationaleForRecommendation: z.string().describe("Rationale for Recommendation (Brief)"),
    proposedNextSteps: z.string().describe("Proposed Next Steps"),
  }).describe("Key Questions & Next Steps section"),
});

export type CIMReview = z.infer<typeof cimReviewSchema>; 