"use client";

import { SelectField } from "@/components/ui/Field";
import type { CollectiveAgreement, Weekday, WorkerType } from "@/lib/supabase/types";

const WEEKDAYS: Weekday[] = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"];

export type RoleEmploymentValue = {
  workerType: WorkerType;
  collectiveAgreement: CollectiveAgreement;
  workDays: Weekday[];
  isBetriebsrat: boolean;
  hasDienstwagen: boolean;
  isLateraleFuehrung: boolean;
  isCLevel: boolean;
};

// Shared by the hire wizard (StepVertrag) and DatenAendernPanel — both edit
// the same set of employees columns, just against different local state.
export function RoleEmploymentFields({ value, onChange }: { value: RoleEmploymentValue; onChange: (patch: Partial<RoleEmploymentValue>) => void }) {
  function toggleWorkDay(day: Weekday) {
    onChange({ workDays: value.workDays.includes(day) ? value.workDays.filter((d) => d !== day) : [...value.workDays, day] });
  }

  return (
    <div className="flex flex-col gap-3">
      <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
        <SelectField
          label="Angestellte:r / Arbeiter:in"
          dense
          value={value.workerType}
          onChange={(v) => onChange({ workerType: v as WorkerType })}
          options={[
            { value: "Angestellte:r", label: "Angestellte:r" },
            { value: "Arbeiter:in", label: "Arbeiter:in" },
          ]}
        />
        <SelectField
          label="Kollektivvertrag"
          dense
          value={value.collectiveAgreement}
          onChange={(v) => onChange({ collectiveAgreement: v as CollectiveAgreement })}
          options={[
            { value: "Handel", label: "Handel" },
            { value: "Süßwaren", label: "Süßwaren" },
          ]}
        />
      </div>

      {/* Toggle group, not a set of fields: a fieldset names the group, and
          aria-pressed is what tells a screen reader a day is selected —
          colour alone does not. */}
      <fieldset>
        <legend className="mb-1 block text-xs font-semibold text-ink-muted">Arbeitstage</legend>
        <div className="flex flex-wrap gap-1.5">
          {WEEKDAYS.map((day) => (
            <button
              key={day}
              type="button"
              aria-pressed={value.workDays.includes(day)}
              onClick={() => toggleWorkDay(day)}
              className={`rounded-full px-3 py-1.5 text-xs font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-500 ${
                value.workDays.includes(day) ? "bg-brand-500 text-white" : "border border-border text-ink-muted hover:bg-surface"
              }`}
            >
              {day}
            </button>
          ))}
        </div>
      </fieldset>

      <div className="flex flex-col gap-2">
        <label className="flex items-center gap-2 text-sm text-ink-body">
          <input type="checkbox" checked={value.isBetriebsrat} onChange={(e) => onChange({ isBetriebsrat: e.target.checked })} />
          Betriebsrat
        </label>
        <label className="flex items-center gap-2 text-sm text-ink-body">
          <input type="checkbox" checked={value.hasDienstwagen} onChange={(e) => onChange({ hasDienstwagen: e.target.checked })} />
          Dienstwagen
        </label>
        <label className="flex items-center gap-2 text-sm text-ink-body">
          <input type="checkbox" checked={value.isLateraleFuehrung} onChange={(e) => onChange({ isLateraleFuehrung: e.target.checked })} />
          Laterale Führung
        </label>
        <label className="flex items-center gap-2 text-sm text-ink-body">
          <input type="checkbox" checked={value.isCLevel} onChange={(e) => onChange({ isCLevel: e.target.checked })} />
          C-Level
        </label>
      </div>
    </div>
  );
}
