import type { AisTarget } from "../../../entities/aisTarget/model/types";
import type { LegacyVesselInfo } from "../../../entities/legacyVessel/model/types";
import type { ZoneId } from "../../../entities/zone/model/meta";
import type { VesselTypeCode } from "../../../entities/vessel/model/types";

export type DerivedVesselState = {
  label: string;
  isFishing: boolean;
  isTransit: boolean;
};

export type DerivedLegacyVessel = {
  mmsi: number;
  name: string;
  callsign: string | null;
  lat: number;
  lon: number;
  sog: number | null;
  cog: number | null;
  heading: number | null;
  messageTimestamp: string | null;
  receivedDate: string | null;

  ais: AisTarget;
  legacy: LegacyVesselInfo;

  shipCode: VesselTypeCode;
  permitNo: string;
  ownerKey: string | null;
  ownerCn: string | null;
  ownerRoman: string | null;
  workSeaArea: string | null;
  pairPermitNo: string | null;

  zoneId: ZoneId | null;
  state: DerivedVesselState;
};

export type PairLink = {
  aMmsi: number;
  bMmsi: number;
  from: [number, number]; // [lon, lat]
  to: [number, number];
  distanceNm: number;
  warn: boolean;
};

export type FcLink = {
  fcMmsi: number;
  otherMmsi: number;
  from: [number, number];
  to: [number, number];
  distanceNm: number;
  suspicious: boolean;
};

export type FleetCircle = {
  ownerKey: string;
  ownerLabel: string;
  center: [number, number];
  radiusNm: number;
  count: number;
  vesselMmsis: number[];
};

export type LegacyAlarmKind = "pair_separation" | "transshipment" | "closed_season" | "ais_stale" | "zone_violation";

export type LegacyAlarm = {
  severity: "cr" | "hi";
  kind: LegacyAlarmKind;
  timeLabel: string;
  text: string;
  relatedMmsi: number[];
};

export const LEGACY_ALARM_KINDS: LegacyAlarmKind[] = [
  "pair_separation",
  "transshipment",
  "closed_season",
  "ais_stale",
  "zone_violation",
];

export const LEGACY_ALARM_KIND_LABEL: Record<LegacyAlarmKind, string> = {
  pair_separation: "쌍 이격 경고",
  transshipment: "환적 의심",
  closed_season: "휴어기 조업 의심",
  ais_stale: "AIS 지연",
  zone_violation: "수역 이탈",
};

/** 경고 우선순위 (낮→높). 배열 뒤가 높은 우선순위. */
export const ALARM_KIND_PRIORITY: LegacyAlarmKind[] = [
  "ais_stale",
  "closed_season",
  "transshipment",
  "zone_violation",
  "pair_separation",
];

/** 경고 배지 — 지도 위 선박 옆에 표시할 약어 + 색상 */
export const ALARM_BADGE: Record<LegacyAlarmKind, { label: string; color: string; rgba: [number, number, number, number] }> = {
  pair_separation: { label: "이", color: "#ef4444", rgba: [239, 68, 68, 200] },
  zone_violation:  { label: "수", color: "#a855f7", rgba: [168, 85, 247, 200] },
  transshipment:   { label: "환", color: "#f97316", rgba: [249, 115, 22, 200] },
  closed_season:   { label: "휴", color: "#eab308", rgba: [234, 179, 8, 200] },
  ais_stale:       { label: "A",  color: "#6b7280", rgba: [107, 114, 128, 200] },
};
