import { Injectable, Logger } from "@nestjs/common";
import { DataSource } from "typeorm";

import {
  DropdownSetting,
  DropdownSettingMeta,
} from "../modules/dropdown-settings/entities/dropdown-setting.entity";

interface DefaultDropdownSetting {
  code: string;
  label: string;
  description: string;
  multiple: boolean;
  meta?: DropdownSettingMeta | null;
}

/**
 * Known dropdown settings, seeded as empty catalogs (no options). The options
 * are managed by the admin from the backoffice Dropdown Settings editor.
 */
const DEFAULT_DROPDOWN_SETTINGS: DefaultDropdownSetting[] = [
  {
    code: "stations_ter",
    label: "Stations TER",
    description:
      "Temporary freight station list used by booking origin and destination yards.",
    multiple: false,
    meta: { searchable: true, clearable: true, version: "temporary" },
  },
  {
    code: "general_contract_period",
    label: "General Contract Period (months)",
    description:
      "How many months a general contract stays open for ordering after activation.",
    multiple: false,
  },
  {
    code: "contract_validity_periods",
    label: "Contract Validity Periods (days)",
    description:
      "Validity durations (in days) a staff can choose when accepting a submitted contract.",
    multiple: false,
  },
  {
    code: "ro_vessel_min_days",
    label: "RO vessel minimum lead time (days)",
    description:
      "Minimum days between today and the vessel departure date on an export Release Order.",
    multiple: false,
  },
  {
    code: "import_train_numbers",
    label: "Import train numbers",
    description:
      "Even IMPORT run numbers (Djibouti → Ethiopia) selectable when building a train. The paired export number is derived automatically (import − 1).",
    multiple: false,
    meta: { searchable: true, clearable: true },
  },
];

@Injectable()
export class DropdownSettingsSeeder {
  private readonly logger = new Logger(DropdownSettingsSeeder.name);

  constructor(private readonly dataSource: DataSource) {}

  async run() {
    const settingRepository = this.dataSource.getRepository(DropdownSetting);

    // Seed only into an empty table: any existing rows (including
    // soft-deleted ones, which would still conflict on the unique `code`)
    // mean the data is admin-managed, so leave it untouched.
    const existing = await settingRepository.count({ withDeleted: true });
    if (existing > 0) {
      this.logger.log(
        `dropdown_settings already has ${existing} rows — skipping seed`,
      );
      return;
    }

    // Insert setting rows only — no DropdownOption rows. Options start empty
    // and are configured by the admin from the backoffice editor.
    await settingRepository.insert(
      DEFAULT_DROPDOWN_SETTINGS.map((setting) => ({
        code: setting.code,
        label: setting.label,
        description: setting.description,
        multiple: setting.multiple,
        meta: setting.meta ?? null,
      })),
    );

    this.logger.log(
      `Seeded ${DEFAULT_DROPDOWN_SETTINGS.length} dropdown settings with empty options`,
    );
  }
}
