openapi: 3.1.0
info:
  title: AGNET Farmer Dashboard v2 Contract
  version: 2.0.0
  description: |
    Canonical extension contract for `GET /farmer/dashboard` module enrichments.

    **Authority:** This file is the single source of truth for dashboard v2 fields.
    TypeScript mirrors live in `lib/api-types.ts`; view models in `lib/farmer-dashboard.ts`;
    fixtures in `lib/fixtures/dashboard-mockup-personas.ts`.

    Parent BFF spec: `../openapi/bff-api.v1.yaml` (base `FarmerDashboardResponse`).

    ## Sync gates
    - G0: This contract merges before UI or backend module work.
    - Agent F (core-api): implements DB → response mapping per `dashboard-v2-db-delta.md`.
    - Agent MOCK: imports persona fixtures verbatim when `CORE_API_USE_MOCK=true`.

servers:
  - url: /api
    description: AGNET BFF (Next.js)

paths:
  /farmer/dashboard:
    get:
      operationId: getFarmerDashboardV2
      summary: Farmer dashboard with Identity, Finance, and Inputs module enrichments
      tags: [farmer]
      security:
        - cookieAuth: []
      responses:
        "200":
          description: Dashboard payload with v2 module extensions
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/FarmerDashboardResponseV2"
        "401":
          description: Unauthorized

components:
  securitySchemes:
    cookieAuth:
      type: apiKey
      in: cookie
      name: agnet_token

  schemas:
    FarmerDashboardResponseV2:
      allOf:
        - $ref: "../openapi/bff-api.v1.yaml#/components/schemas/FarmerDashboardResponse"
        - type: object
          properties:
            identity:
              nullable: true
              $ref: "#/components/schemas/IdentityModuleV2"
            finance:
              nullable: true
              $ref: "#/components/schemas/FinanceModuleV2"
            inputs:
              nullable: true
              $ref: "#/components/schemas/InputsModuleV2"

    GeoJsonPolygon:
      type: object
      required: [type, coordinates]
      properties:
        type:
          type: string
          enum: [Polygon]
        coordinates:
          type: array
          items:
            type: array
            items:
              type: array
              minItems: 2
              maxItems: 2
              items:
                type: number

    IdentityEkyc:
      type: object
      properties:
        photoUrl:
          type: string
          format: uri
        dob:
          type: string
          description: ISO date (YYYY-MM-DD)
        aadhaarMasked:
          type: string
          description: Masked national ID / PINFL (e.g. XXXX-XXXX-4521)
        mobileMasked:
          type: string
          description: Masked phone (e.g. +998 ** *** 45 67)

    IdentityGis:
      type: object
      required: [areaHa, lat, lng]
      properties:
        areaHa:
          type: number
          minimum: 0
        lat:
          type: number
        lng:
          type: number
        polygonGeoJson:
          $ref: "#/components/schemas/GeoJsonPolygon"

    LandEvidenceItem:
      type: object
      required: [name, type, verified]
      properties:
        name:
          type: string
        type:
          type: string
          enum: [pdf, doc]
        verified:
          type: boolean

    YieldHistoryEntry:
      type: object
      required: [year, yieldTHa, crop]
      properties:
        year:
          type: integer
        yieldTHa:
          type: number
        crop:
          type: string

    ComplianceItem:
      type: object
      required: [key, label, passed]
      properties:
        key:
          type: string
        label:
          type: string
        passed:
          type: boolean

    IdentityModuleV2:
      allOf:
        - $ref: "../openapi/bff-api.v1.yaml#/components/schemas/IdentityModule"
        - type: object
          properties:
            farmerId:
              type: string
              pattern: "^AGID-FMR-[A-Z0-9-]+$"
              example: AGID-FMR-00142
            farmName:
              type: string
            ekyc:
              $ref: "#/components/schemas/IdentityEkyc"
            gis:
              $ref: "#/components/schemas/IdentityGis"
            landEvidence:
              type: array
              items:
                $ref: "#/components/schemas/LandEvidenceItem"
            yieldHistory:
              type: array
              items:
                $ref: "#/components/schemas/YieldHistoryEntry"
            compliance:
              type: array
              items:
                $ref: "#/components/schemas/ComplianceItem"

    RoutedInstitution:
      type: object
      required: [name, priority, isBestMatch]
      properties:
        name:
          type: string
        priority:
          type: string
        isBestMatch:
          type: boolean

    FinanceSla:
      type: object
      required: [remainingHours, totalHours, dueAt]
      properties:
        remainingHours:
          type: number
        totalHours:
          type: number
        dueAt:
          type: string
          format: date-time

    BankMatch:
      type: object
      required: [name, score, status]
      properties:
        name:
          type: string
        score:
          type: number
          minimum: 0
          maximum: 100
        status:
          type: string
          enum: [routed, view]

    FinanceDocumentItem:
      type: object
      required: [name, status]
      properties:
        name:
          type: string
        status:
          type: string
          enum: [verified, pending]

    FinanceModuleV2:
      allOf:
        - $ref: "../openapi/bff-api.v1.yaml#/components/schemas/FinanceModule"
        - type: object
          properties:
            eligibilityScore:
              type: number
              minimum: 0
              maximum: 100
            eligibilityLabel:
              type: string
            routedInstitution:
              $ref: "#/components/schemas/RoutedInstitution"
            sla:
              $ref: "#/components/schemas/FinanceSla"
            bankMatches:
              type: array
              items:
                $ref: "#/components/schemas/BankMatch"
            documents:
              type: array
              items:
                $ref: "#/components/schemas/FinanceDocumentItem"

    InputsKpis:
      type: object
      required: [seed, fertiliser, equipment, vouchers, inTransit]
      properties:
        seed:
          type: integer
          minimum: 0
        fertiliser:
          type: integer
          minimum: 0
        equipment:
          type: integer
          minimum: 0
        vouchers:
          type: integer
          minimum: 0
        inTransit:
          type: integer
          minimum: 0

    SupplierRating:
      type: object
      required: [name, rating]
      properties:
        name:
          type: string
        logoUrl:
          type: string
          format: uri
        rating:
          type: number
          minimum: 0
          maximum: 5

    CatalogItem:
      type: object
      required: [id, name, unitPrice, currency]
      properties:
        id:
          type: string
        name:
          type: string
        unitPrice:
          type: number
        currency:
          type: string
        imageUrl:
          type: string
          format: uri

    CatalogCategory:
      type: object
      required: [category, items]
      properties:
        category:
          type: string
        items:
          type: array
          items:
            $ref: "#/components/schemas/CatalogItem"

    VoucherPaymentRoute:
      type: object
      required: [step, label, completed]
      properties:
        step:
          type: integer
        label:
          type: string
        completed:
          type: boolean

    VoucherDetail:
      type: object
      required: [code, amount, currency, status, paymentRoute]
      properties:
        code:
          type: string
        amount:
          type: number
        currency:
          type: string
        status:
          type: string
          enum: [active, redeemed, expired]
        paymentRoute:
          type: array
          items:
            $ref: "#/components/schemas/VoucherPaymentRoute"

    DeliveryStep:
      type: object
      required: [label, completed, timestamp]
      properties:
        label:
          type: string
        completed:
          type: boolean
        timestamp:
          type: string
          format: date-time
          nullable: true

    DeliveryTracking:
      type: object
      required: [trackingId, status, steps]
      properties:
        trackingId:
          type: string
        status:
          type: string
          enum: [pending, in_transit, delivered]
        eta:
          type: string
          format: date-time
        steps:
          type: array
          items:
            $ref: "#/components/schemas/DeliveryStep"

    InputsModuleV2:
      allOf:
        - $ref: "../openapi/bff-api.v1.yaml#/components/schemas/InputsModule"
        - type: object
          properties:
            kpis:
              $ref: "#/components/schemas/InputsKpis"
            suppliers:
              type: array
              items:
                $ref: "#/components/schemas/SupplierRating"
            catalog:
              type: array
              items:
                $ref: "#/components/schemas/CatalogCategory"
            voucher:
              $ref: "#/components/schemas/VoucherDetail"
            delivery:
              $ref: "#/components/schemas/DeliveryTracking"
