components:
  schemas:
    StepAccountData:
      type: object
      required: [phone, verified]
      properties:
        phone:
          type: string
          description: E.164 format +998[0-9]{9}
          pattern: "^\\+998[0-9]{9}$"
          example: "+998901234567"
        verified:
          type: boolean
          description: true after OTP verification
        verificationMethod:
          $ref: "./enums.yaml#/components/schemas/VerificationMethod"

    ConsentAcceptedMap:
      type: object
      description: Map of consent key → accepted; all required P0 keys must be true
      required:
        - credit_bureau
        - gis_satellite
        - data_sharing_institutions
      properties:
        credit_bureau:
          type: boolean
        gis_satellite:
          type: boolean
        data_sharing_institutions:
          type: boolean
      additionalProperties:
        type: boolean

    StepConsentData:
      type: object
      required: [accepted, consentVersion, consentType, acceptedAt, locale]
      properties:
        accepted:
          $ref: "#/components/schemas/ConsentAcceptedMap"
        consentVersion:
          type: string
          description: Semver legal version
          example: "1.0.0"
        consentType:
          type: string
          example: credit_check
        acceptedAt:
          type: string
          format: date-time
        locale:
          $ref: "./enums.yaml#/components/schemas/ConsentLocale"

    StepKycData:
      type: object
      required: [nationalId, idType, kycStatus, verifiedAt]
      properties:
        nationalId:
          type: string
          description: PINFL — 14 digits
          pattern: "^[0-9]{14}$"
        pinfl:
          type: string
          description: Alias for nationalId
          pattern: "^[0-9]{14}$"
        idType:
          $ref: "./enums.yaml#/components/schemas/IdType"
        oneIdSessionId:
          type: string
          format: uuid
        fullNameOfficial:
          type: string
          description: Read-only from national registry
          readOnly: true
        dateOfBirthOfficial:
          type: string
          format: date
          readOnly: true
        genderOfficial:
          $ref: "./enums.yaml#/components/schemas/Gender"
          readOnly: true
        kycStatus:
          $ref: "./enums.yaml#/components/schemas/KycStatus"
        verifiedAt:
          type: string
          format: date-time

    StepFarmProfileData:
      type: object
      required:
        - fullName
        - dateOfBirth
        - gender
        - primaryLanguage
        - region
        - district
        - householdSize
        - yearsFarming
        - farmerType
        - education
      properties:
        fullName:
          type: string
          minLength: 2
          maxLength: 100
        dateOfBirth:
          type: string
          format: date
        gender:
          $ref: "./enums.yaml#/components/schemas/Gender"
        primaryLanguage:
          $ref: "./enums.yaml#/components/schemas/PreferredLanguage"
        region:
          type: string
          description: Reference value from GET /reference/options?type=regions
        district:
          type: string
          description: Reference value from GET /reference/options?type=districts
        householdSize:
          type: integer
          minimum: 1
          maximum: 20
        yearsFarming:
          type: integer
          minimum: 0
          maximum: 60
        farmerType:
          $ref: "./enums.yaml#/components/schemas/FarmerType"
        education:
          $ref: "./enums.yaml#/components/schemas/Education"

    LandParcel:
      type: object
      required:
        - district
        - village
        - areaHa
        - tenureType
        - landUseType
        - irrigationSource
        - isPrimary
      properties:
        district:
          type: string
          description: Reference value from GET /reference/options?type=districts
        village:
          type: string
          minLength: 2
          maxLength: 80
        areaHa:
          type: number
          format: double
          minimum: 0.01
          maximum: 500
        tenureType:
          $ref: "./enums.yaml#/components/schemas/TenureType"
        landUseType:
          $ref: "./enums.yaml#/components/schemas/LandUseType"
        irrigationSource:
          $ref: "./enums.yaml#/components/schemas/IrrigationSource"
        isPrimary:
          type: boolean
          description: Exactly one parcel in the array must be true
        registryRef:
          type: string
          description: Kadastr reference or empty

    StepLandSketchData:
      type: object
      required: [parcels]
      properties:
        parcels:
          type: array
          minItems: 1
          items:
            $ref: "#/components/schemas/LandParcel"

    GisCoordinates:
      type: object
      properties:
        lat:
          type: number
          format: double
        lng:
          type: number
          format: double
        accuracyM:
          type: number
          description: Geolocation API accuracy in meters

    StepGisMappingData:
      type: object
      required: [gisStatus]
      properties:
        gisStatus:
          $ref: "./enums.yaml#/components/schemas/GisStatus"
        parcelId:
          type: string
          description: FK to primary parcel in LAND_SKETCH
        coordinates:
          $ref: "#/components/schemas/GisCoordinates"
        note:
          type: string
          maxLength: 200
        captureMethod:
          $ref: "./enums.yaml#/components/schemas/CaptureMethod"

    CropEntry:
      type: object
      required: [crop, season, areaHa, expectedYield]
      properties:
        crop:
          type: string
          description: Reference value from GET /reference/options?type=crops
        season:
          $ref: "./enums.yaml#/components/schemas/CropSeason"
        areaHa:
          type: number
          format: double
          minimum: 0
        expectedYield:
          type: number
          format: double
          minimum: 0
          exclusiveMinimum: true

    StepCropPlanData:
      type: object
      required: [crops]
      properties:
        crops:
          type: array
          minItems: 1
          items:
            $ref: "#/components/schemas/CropEntry"

    AssetEntry:
      type: object
      required: [assetType, condition, estimatedValue, quantity]
      properties:
        assetType:
          type: string
          description: Reference value from GET /reference/options?type=asset_types
        condition:
          $ref: "./enums.yaml#/components/schemas/AssetCondition"
        estimatedValue:
          type: number
          format: double
          minimum: 0
        quantity:
          type: integer
          minimum: 1
          default: 1

    StepFarmAssetsData:
      type: object
      properties:
        assets:
          type: array
          items:
            $ref: "#/components/schemas/AssetEntry"
        totalAssetValueUsd:
          type: number
          format: double
          minimum: 0
          description: Computed sum of asset values in USD
        noneDeclared:
          type: boolean
          description: Shortcut when farmer has no assets to declare

    StepFinanceRequestData:
      type: object
      required:
        - purpose
        - amountUzs
        - termMonths
        - monthlyIncomeUzs
        - existingLoansUzs
        - repaymentHistory
      properties:
        purpose:
          type: string
          description: Reference value from GET /reference/options?type=finance_purposes
        amountUzs:
          type: number
          format: double
          minimum: 500000
        amountUsd:
          type: number
          format: double
          minimum: 0
          description: Computed from FX snapshot
        termMonths:
          type: integer
          minimum: 3
          maximum: 60
        monthlyIncomeUzs:
          type: number
          format: double
          minimum: 0
        existingLoansUzs:
          type: number
          format: double
          minimum: 0
        repaymentHistory:
          $ref: "./enums.yaml#/components/schemas/RepaymentHistory"

    DocumentRecord:
      type: object
      properties:
        fileId:
          type: string
        uploadedAt:
          type: string
          format: date-time

    ExtractedFinancials:
      type: object
      properties:
        monthlyIncomeUzs:
          type: number
          format: double
          minimum: 0
        existingLoansUzs:
          type: number
          format: double
          minimum: 0
        accountBalanceUzs:
          type: number
          format: double
          minimum: 0

    StepDocumentsData:
      type: object
      properties:
        uploaded:
          type: object
          additionalProperties:
            type: boolean
          description: Map of docType → uploaded flag
        documents:
          type: array
          items:
            allOf:
              - type: object
                required: [docType]
                properties:
                  docType:
                    $ref: "./enums.yaml#/components/schemas/DocumentType"
              - $ref: "#/components/schemas/DocumentRecord"
        extractionStatus:
          $ref: "./enums.yaml#/components/schemas/ExtractionStatus"
        extractedFinancials:
          $ref: "#/components/schemas/ExtractedFinancials"
        documentQualityScore:
          type: number
          format: double
          minimum: 0
          maximum: 100

    StepReviewData:
      type: object
      required: [reviewed, reviewedAt]
      properties:
        reviewed:
          type: boolean
        reviewedAt:
          type: string
          format: date-time
        attestationText:
          type: string
          description: CMS version reference for legal attestation

    StepScoringData:
      type: object
      properties:
        scoringComplete:
          type: boolean
        scoringError:
          type: boolean
        decision:
          $ref: "./enums.yaml#/components/schemas/EligibilityOutcome"
        score:
          type: integer
          minimum: 0
          maximum: 1000
        reasonCodes:
          type: array
          items:
            $ref: "#/components/schemas/ReasonCode"
        financeCaseId:
          type: string
        financeCaseStatus:
          $ref: "./enums.yaml#/components/schemas/FinanceCaseStatus"
        conditionalRequirements:
          type: array
          items:
            $ref: "#/components/schemas/ConditionalRequirement"
        satelliteSummary:
          type: string
        farmerExplanation:
          type: object
          properties:
            uz:
              type: string
            ru:
              type: string
            en:
              type: string
        satelliteImages:
          type: array
          items:
            type: string
            format: uri
        scoringMeta:
          type: object
          additionalProperties: true

    ConditionalRequirement:
      type: object
      properties:
        code:
          type: string
        label:
          type: string
        description:
          type: string

    StepResultData:
      type: object
      required: [acknowledged, acknowledgedAt]
      properties:
        acknowledged:
          type: boolean
        acknowledgedAt:
          type: string
          format: date-time
        outcome:
          $ref: "./enums.yaml#/components/schemas/EligibilityOutcome"
        financeCaseId:
          type: string
        financeCaseStatus:
          $ref: "./enums.yaml#/components/schemas/FinanceCaseStatus"
        conditionalRequirements:
          type: array
          items:
            $ref: "#/components/schemas/ConditionalRequirement"

    ReasonCode:
      type: object
      properties:
        code:
          type: string
          example: RC_NDVI_ANOMALY
        feature:
          type: string
        contribution:
          type: number
          format: float
        direction:
          type: string
          enum: [positive, negative]

    OnboardingStepData:
      description: Map of step key to step-specific payload stored in session.stepData
      type: object
      additionalProperties: true
      properties:
        ACCOUNT:
          $ref: "#/components/schemas/StepAccountData"
        CONSENT:
          $ref: "#/components/schemas/StepConsentData"
        KYC:
          $ref: "#/components/schemas/StepKycData"
        FARM_PROFILE:
          $ref: "#/components/schemas/StepFarmProfileData"
        LAND_SKETCH:
          $ref: "#/components/schemas/StepLandSketchData"
        GIS_MAPPING:
          $ref: "#/components/schemas/StepGisMappingData"
        CROP_PLAN:
          $ref: "#/components/schemas/StepCropPlanData"
        FARM_ASSETS:
          $ref: "#/components/schemas/StepFarmAssetsData"
        DOCUMENTS:
          $ref: "#/components/schemas/StepDocumentsData"
        FINANCE_REQUEST:
          $ref: "#/components/schemas/StepFinanceRequestData"
        REVIEW:
          $ref: "#/components/schemas/StepReviewData"
        SCORING:
          $ref: "#/components/schemas/StepScoringData"
        RESULT:
          $ref: "#/components/schemas/StepResultData"
