/** Shared field/column builders for Hospitality resources. */
import type { HospitalityColumnConfig, HospitalityFieldConfig } from "@/modules/hospitality/components/HospitalityListCrudPage";
import { lifecycleStatusLabels, venueFormatLabels } from "@/modules/hospitality/design-system/tokens";

export const lifecycleStatusOptions = Object.entries(lifecycleStatusLabels).map(([value, label]) => ({
  value,
  label,
}));

export const venueFormatOptions = Object.entries(venueFormatLabels).map(([value, label]) => ({
  value,
  label,
}));

export function auditColumns(): HospitalityColumnConfig[] {
  return [
    { key: "created_at", header: "ایجاد", type: "datetime", defaultVisible: false },
    { key: "updated_at", header: "به‌روزرسانی", type: "datetime", defaultVisible: false },
  ];
}

export function codeNameStatusColumns(extra: HospitalityColumnConfig[] = []): HospitalityColumnConfig[] {
  return [
    { key: "code", header: "کد" },
    { key: "name", header: "نام" },
    ...extra,
    { key: "status", header: "وضعیت", type: "status" },
    ...auditColumns(),
  ];
}

export function codeNameCreateFields(extra: HospitalityFieldConfig[] = []): HospitalityFieldConfig[] {
  return [
    { name: "code", label: "کد", required: true },
    { name: "name", label: "نام", required: true },
    ...extra,
  ];
}

export function venueIdField(required = true): HospitalityFieldConfig {
  return { name: "venue_id", label: "مکان (Venue ID)", type: "venue", required };
}

export function formatPrice(row: Record<string, unknown>, key = "base_price"): string {
  const amount = Number(row[key] ?? 0);
  const currency = String(row.currency_code ?? "IRR");
  return `${amount.toLocaleString("fa-IR")} ${currency}`;
}

export function countByStatus(rows: Record<string, unknown>[], statusKey = "status") {
  const total = rows.length;
  const active = rows.filter((r) => r[statusKey] === "active" || r[statusKey] === "published").length;
  const draft = rows.filter((r) => r[statusKey] === "draft").length;
  return { total, active, draft };
}
