{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Charter Party Bulk Upload",
  "description": "Schema for bulk upload of charter party agreements",
  "type": "array",
  "items": {
    "type": "object",
    "required": [
      "authority",
      "ship_call_sign",
      "usage_type",
      "start_date",
      "end_date",
      "start_port_in_greece",
      "end_port_in_greece",
      "captain_first_name",
      "captain_last_name",
      "captain_email"
    ],
    "properties": {
      "authority": {
        "type": "string",
        "description": "Authority responsible for the charter party",
        "minLength": 1,
        "examples": ["lch-milou", "kl-chiou"]
      },
      "ship_call_sign": {
        "type": "string",
        "description": "Call sign of the vessel",
        "minLength": 1,
        "examples": ["SY1234", "SV5678"]
      },
      "usage_type": {
        "type": "string",
        "enum": ["exploitation", "self_use", "empty_run"],
        "description": "Type of vessel usage"
      },
      "start_date": {
        "type": "string",
        "format": "date-time",
        "description": "Charter start date and time (ISO 8601 format)"
      },
      "end_date": {
        "type": "string",
        "format": "date-time",
        "description": "Charter end date and time (ISO 8601 format)"
      },
      "start_port_in_greece": {
        "type": "boolean",
        "description": "Is the departure port located within Greek territory?"
      },
      "end_port_in_greece": {
        "type": "boolean",
        "description": "Is the arrival port located within Greek territory?"
      },
      "start_port_code": {
        "type": "string",
        "description": "Departure port code (required if start_port_in_greece is true)",
        "examples": ["GRPIR", "GRATH", "GRMYK"]
      },
      "end_port_code": {
        "type": "string",
        "description": "Arrival port code (required if end_port_in_greece is true)",
        "examples": ["GRPIR", "GRATH", "GRMYK"]
      },
      "start_port_foreign": {
        "type": "string",
        "description": "Departure port name (required if start_port_in_greece is false)",
        "examples": ["Venice, Italy", "Marina di Ragusa, Italy"]
      },
      "end_port_foreign": {
        "type": "string",
        "description": "Arrival port name (required if end_port_in_greece is false)",
        "examples": ["Venice, Italy", "Marina di Ragusa, Italy"]
      },
      "captain_first_name": {
        "type": "string",
        "description": "Captain's first name",
        "minLength": 1
      },
      "captain_last_name": {
        "type": "string",
        "description": "Captain's last name",
        "minLength": 1
      },
      "captain_email": {
        "type": "string",
        "format": "email",
        "description": "Captain's email address for system access"
      },
      "client_id": {
        "type": ["string", "number"],
        "description": "Optional. A client-provided identifier for this specific charter record. Used for traceability and error reporting."
      },
      "parties": {
        "type": "array",
        "description": "List of parties involved in the charter (required for exploitation)",
        "items": {
          "type": "object",
          "required": ["party_role", "party_type", "name"],
          "properties": {
            "party_role": {
              "type": "string",
              "enum": ["charterer", "broker", "authorized"],
              "description": "Role of the party in the charter agreement"
            },
            "party_type": {
              "type": "string",
              "enum": ["physical", "legal"],
              "description": "Type of party: physical person or legal entity"
            },
            "tax_id": {
              "type": "string",
              "description": "Tax identification number (ΑΦΜ)"
            },
            "name": {
              "type": "string",
              "description": "Full name (physical) or company name (legal)",
              "minLength": 1
            },
            "address": {
              "type": "string",
              "description": "Full address"
            },
            "id_number": {
              "type": "string",
              "description": "ID card number (for physical persons)"
            },
            "phone": {
              "type": "string",
              "description": "Contact phone number"
            },
            "email": {
              "type": "string",
              "format": "email",
              "description": "Contact email address"
            },
            "legal_rep_name": {
              "type": "string",
              "description": "Legal representative name (for legal entities)"
            }
          }
        }
      },
      "freight": {
        "type": "number",
        "minimum": 0,
        "description": "Total charter fee in EUR"
      },
      "execution_subtype": {
        "type": "string",
        "enum": ["ba", "bb"],
        "description": "Exception subtype for exploitation charters"
      },
      "fuel_burden": {
        "type": "string",
        "enum": ["charterer", "owner"],
        "description": "Who bears the fuel costs including special consumption tax"
      },
      "additional_expenses_on_owner": {
        "type": "boolean",
        "description": "Are additional expenses borne by the owner?"
      },
      "additional_terms": {
        "type": "string",
        "description": "Additional terms and conditions for the charter"
      },
      "empty_run_movement": {
        "type": "string",
        "enum": [
          "CHANGE_BERTH",
          "MOORING_TRIAL",
          "SHIPYARD_MOVE",
          "CHARTER_PORT",
          "SUPPLYING",
          "EXHIBITION",
          "OWNER_TRANSFER"
        ],
        "description": "Type of empty run movement (required for empty_run usage type)"
      },
      "crew": {
        "type": "array",
        "description": "Optional. List of crew members.",
        "items": {
          "$ref": "#/$defs/personRecord"
        }
      },
      "passengers": {
        "type": "array",
        "description": "Optional. List of passengers.",
        "items": {
          "$ref": "#/$defs/personRecord"
        }
      }
    },
    "allOf": [
      {
        "if": {
          "properties": {
            "start_port_in_greece": { "const": true }
          }
        },
        "then": {
          "required": ["start_port_code"]
        }
      },
      {
        "if": {
          "properties": {
            "start_port_in_greece": { "const": false }
          }
        },
        "then": {
          "required": ["start_port_foreign"]
        }
      },
      {
        "if": {
          "properties": {
            "end_port_in_greece": { "const": true }
          }
        },
        "then": {
          "required": ["end_port_code"]
        }
      },
      {
        "if": {
          "properties": {
            "end_port_in_greece": { "const": false }
          }
        },
        "then": {
          "required": ["end_port_foreign"]
        }
      },
      {
        "if": {
          "properties": {
            "usage_type": { "const": "empty_run" }
          }
        },
        "then": {
          "required": ["empty_run_movement"]
        }
      }
    ]
  },
  "minItems": 1,
  "$defs": {
    "personRecord": {
      "type": "object",
      "required": [
        "full_name",
        "id_number",
        "sex",
        "nationality",
        "date_of_birth",
        "embarkation_date",
        "embarkation_port"
      ],
      "properties": {
        "full_name": { "type": "string", "minLength": 1 },
        "id_number": { "type": "string", "minLength": 1 },
        "sex": { "type": "string", "enum": ["M", "F"] },
        "nationality": { "type": "string", "minLength": 1 },
        "date_of_birth": { "type": "string", "format": "date-time" },
        "embarkation_date": { "type": "string", "format": "date-time" },
        "embarkation_port": { "type": "string", "minLength": 1 },
        "notes": { "type": "string" }
      }
    }
  }
}
