// ponytail: in-memory mock array, resets on reload — swap for RTK Query endpoint when backend lands.

export type VesselCategory = 'Inland Waterway' | 'Sea-going';

export const VESSEL_TYPES: Record<VesselCategory, string[]> = {
  'Inland Waterway': ['Passenger Boat', 'Cargo Barge', 'Ferry', 'Tugboat', 'Fishing Boat'],
  'Sea-going': ['Bulk Carrier', 'Container Ship', 'Tanker', 'General Cargo', 'Passenger Ship'],
};

export const ENGINE_TYPES = ['Diesel', 'Inboard', 'Outboard', 'Electric', 'Steam'] as const;

export const HULL_MATERIALS = ['Steel', 'Aluminum', 'Fiberglass', 'Wood', 'Composite'] as const;

export interface RequiredDocSlot {
  key: string;
  label: string;
  minCount?: number;
}

export const REQUIRED_DOCS: Record<VesselCategory, RequiredDocSlot[]> = {
  'Inland Waterway': [{ key: 'photos', label: 'Vessel Photos', minCount: 2 }],
  'Sea-going': [
    { key: 'photos', label: 'Vessel Photos' },
    { key: 'ownership', label: 'Proof of Ownership / Bill of Sale' },
    { key: 'particulars', label: 'Ship Particulars' },
    { key: 'insurance', label: 'Insurance Certificate' },
  ],
};

export const CERTIFICATES: Record<VesselCategory, string[]> = {
  'Inland Waterway': ['Inland Vessel Registration Certificate'],
  'Sea-going': [
    'Certificate of Nationality',
    'Certificate of Ownership',
    'Certificate of Registration',
    'Minimum Safe Manning Certificate',
  ],
};

export type RegistrationStatus = 'Pending' | 'Under Review' | 'Correction Required' | 'Approved' | 'Rejected';

export const STATUS_COLOR: Record<RegistrationStatus, string> = {
  Pending: 'gray',
  'Under Review': 'blue',
  'Correction Required': 'orange',
  Approved: 'teal',
  Rejected: 'red',
};

export type RenewalState = 'OK' | 'Due Soon' | 'Overdue';

export interface RegistrationOwner {
  name: string;
  idOrTin: string;
  phone: string;
  email: string;
  address: string;
}

export interface RegistrationCertificate {
  name: string;
  number: string;
  issueDate: string;
  downloads: number;
}

export interface TimelineStep {
  date: string | null;
  event: string;
  done: boolean;
}

export interface VesselRegistration {
  id: string;
  category: VesselCategory;
  status: RegistrationStatus;
  submitted: string;
  remarks?: string;
  expiryDate?: string;
  renewal?: RenewalState;
  timeline: TimelineStep[];
  certificates?: RegistrationCertificate[];

  // Vessel details
  vesselName: string;
  vesselType: string;
  registrationArea: string;
  flagState: string;
  passengerCapacity?: string;
  grossTonnage?: string;
  length: string;
  breadth: string;
  depth: string;

  // Technical
  imoNumber?: string;
  hullNumber?: string;
  shipyard: string;
  yearBuilt: string;
  engineType: string;
  engineNumber: string;
  enginePower: string;
  hullMaterial: string;

  // Ownership
  owner: RegistrationOwner;
}

const SUBMITTED_STEP = (date: string): TimelineStep => ({ date, event: 'Application Submitted', done: true });
const PENDING_STEP = (event: string): TimelineStep => ({ date: null, event, done: false });

export const MOCK_REGISTRATIONS: VesselRegistration[] = [
  {
    id: 'VR-2025-0001',
    category: 'Sea-going',
    status: 'Under Review',
    submitted: '2025-06-20',
    timeline: [
      SUBMITTED_STEP('2025-06-20'),
      { date: '2025-06-22', event: 'Document Verification', done: true },
      PENDING_STEP('Inspection'),
      PENDING_STEP('Approval'),
    ],
    vesselName: 'MV Nile Star',
    vesselType: 'Bulk Carrier',
    registrationArea: 'Djibouti Corridor',
    flagState: 'Ethiopia',
    grossTonnage: '18500',
    length: '190',
    breadth: '28',
    depth: '15',
    imoNumber: 'IMO9876543',
    shipyard: 'Hyundai Heavy Industries',
    yearBuilt: '2016',
    engineType: 'Diesel',
    engineNumber: 'ENG-44210',
    enginePower: '12000 kW',
    hullMaterial: 'Steel',
    owner: {
      name: 'Nile Shipping PLC',
      idOrTin: 'TIN-0012345678',
      phone: '+251911223344',
      email: 'ops@nileshipping.et',
      address: 'Bole Sub-city, Addis Ababa',
    },
  },
  {
    id: 'VR-2025-0002',
    category: 'Inland Waterway',
    status: 'Correction Required',
    submitted: '2025-06-10',
    remarks: 'Vessel photos are blurry — please re-upload at least 2 clear photos showing the hull and registration markings.',
    timeline: [
      SUBMITTED_STEP('2025-06-10'),
      { date: '2025-06-12', event: 'Document Verification', done: true },
      PENDING_STEP('Inspection'),
      PENDING_STEP('Approval'),
    ],
    vesselName: 'Tana Ferry 3',
    vesselType: 'Ferry',
    registrationArea: 'Lake Tana',
    flagState: 'Ethiopia',
    passengerCapacity: '40',
    length: '18',
    breadth: '5',
    depth: '2',
    hullNumber: 'HN-2211',
    shipyard: 'Bahir Dar Boat Works',
    yearBuilt: '2020',
    engineType: 'Outboard',
    engineNumber: 'ENG-9931',
    enginePower: '150 hp',
    hullMaterial: 'Fiberglass',
    owner: {
      name: 'Getachew Alemu',
      idOrTin: 'ID-4455667788',
      phone: '+251922334455',
      email: 'getachew.alemu@example.com',
      address: 'Bahir Dar, Amhara',
    },
  },
  {
    id: 'VR-2025-0003',
    category: 'Sea-going',
    status: 'Approved',
    submitted: '2025-04-05',
    expiryDate: '2026-08-15',
    renewal: 'Due Soon',
    timeline: [
      SUBMITTED_STEP('2025-04-05'),
      { date: '2025-04-08', event: 'Document Verification', done: true },
      { date: '2025-04-20', event: 'Inspection', done: true },
      { date: '2025-04-28', event: 'Approval', done: true },
    ],
    certificates: [
      { name: 'Certificate of Nationality', number: 'CN-2025-0091', issueDate: '2025-04-28', downloads: 0 },
      { name: 'Certificate of Ownership', number: 'CO-2025-0091', issueDate: '2025-04-28', downloads: 0 },
      { name: 'Certificate of Registration', number: 'CR-2025-0091', issueDate: '2025-04-28', downloads: 0 },
      { name: 'Minimum Safe Manning Certificate', number: 'MSM-2025-0091', issueDate: '2025-04-28', downloads: 0 },
    ],
    vesselName: 'MV Abay Voyager',
    vesselType: 'General Cargo',
    registrationArea: 'Djibouti Corridor',
    flagState: 'Ethiopia',
    grossTonnage: '9600',
    length: '140',
    breadth: '21',
    depth: '11',
    imoNumber: 'IMO9123456',
    shipyard: 'Damen Shipyards',
    yearBuilt: '2012',
    engineType: 'Diesel',
    engineNumber: 'ENG-33012',
    enginePower: '7200 kW',
    hullMaterial: 'Steel',
    owner: {
      name: 'Abay Maritime PLC',
      idOrTin: 'TIN-0098765432',
      phone: '+251933445566',
      email: 'contact@abaymaritime.et',
      address: 'Kirkos Sub-city, Addis Ababa',
    },
  },
  {
    id: 'VR-2025-0004',
    category: 'Inland Waterway',
    status: 'Rejected',
    submitted: '2025-03-02',
    remarks: 'Hull number does not match the submitted proof of ownership. Application rejected — please reapply with matching documentation.',
    timeline: [
      SUBMITTED_STEP('2025-03-02'),
      { date: '2025-03-05', event: 'Document Verification', done: true },
      { date: '2025-03-14', event: 'Inspection', done: true },
      { date: '2025-03-18', event: 'Approval', done: false },
    ],
    vesselName: 'Awash Cargo 1',
    vesselType: 'Cargo Barge',
    registrationArea: 'Awash River Basin',
    flagState: 'Ethiopia',
    passengerCapacity: '0',
    length: '22',
    breadth: '6',
    depth: '3',
    hullNumber: 'HN-1187',
    shipyard: 'Awash River Works',
    yearBuilt: '2018',
    engineType: 'Inboard',
    engineNumber: 'ENG-5567',
    enginePower: '210 hp',
    hullMaterial: 'Steel',
    owner: {
      name: 'Selam Tesfaye',
      idOrTin: 'ID-2233445566',
      phone: '+251944556677',
      email: 'selam.tesfaye@example.com',
      address: 'Adama, Oromia',
    },
  },
];

export function addRegistration(
  reg: Omit<VesselRegistration, 'id' | 'status' | 'submitted' | 'timeline' | 'certificates'>
): VesselRegistration {
  const submitted = new Date().toISOString().slice(0, 10);
  const created: VesselRegistration = {
    ...reg,
    id: `VR-2025-${String(MOCK_REGISTRATIONS.length + 1).padStart(4, '0')}`,
    status: 'Pending',
    submitted,
    timeline: [
      SUBMITTED_STEP(submitted),
      PENDING_STEP('Document Verification'),
      PENDING_STEP('Inspection'),
      PENDING_STEP('Approval'),
    ],
  };
  MOCK_REGISTRATIONS.unshift(created);
  return created;
}

// ponytail: in-memory counter, not persisted.
export function recordDownload(regId: string, certName: string): void {
  const reg = MOCK_REGISTRATIONS.find((r) => r.id === regId);
  const cert = reg?.certificates?.find((c) => c.name === certName);
  if (cert) cert.downloads += 1;
}
