import { Calendar, Save, X } from 'lucide-react';

import { getDraftAttendancePercentage } from '@/business/campus-attendance/selectors';
import type {
  CampusAttendancePageActions,
  CampusAttendancePageState,
} from '@/components/campus-attendance/types';
import { percentageTextClass } from '@/components/campus-attendance/styles';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';

type CampusAttendanceEntryFormProps = {
  state: CampusAttendancePageState;
  actions: CampusAttendancePageActions;
};

export function CampusAttendanceEntryForm({ state, actions }: CampusAttendanceEntryFormProps) {
  const { campusInfo, entryDraft, entryError, saving } = state;
  const draftPercentage = getDraftAttendancePercentage(entryDraft);

  return (
    <div className="bg-slate-800/60 backdrop-blur-sm rounded-2xl border border-orange-500/20 p-6">
      <div className="flex items-center justify-between mb-5">
        <h3 className="font-semibold text-white flex items-center gap-2">
          <Calendar size={18} className="text-orange-400" />
          Enter Attendance for {campusInfo?.fullName}
        </h3>
        <button
          type="button"
          onClick={() => actions.setShowEntryForm(false)}
          className="text-slate-400 hover:text-white transition-colors"
          aria-label="Close attendance entry form"
        >
          <X size={18} />
        </button>
      </div>
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
        <AttendanceEntryInput label="Date" type="date" value={entryDraft.date} onChange={(value) => actions.updateEntryDraft({ date: value })} />
        <AttendanceEntryInput label="Total Enrolled" value={entryDraft.enrolled} placeholder="e.g. 45" onChange={(value) => actions.updateEntryDraft({ enrolled: value })} />
        <AttendanceEntryInput label="Total Present" value={entryDraft.present} placeholder="e.g. 42" onChange={(value) => actions.updateEntryDraft({ present: value })} />
        <AttendanceEntryInput label="Total Absent" value={entryDraft.absent} placeholder="e.g. 2" onChange={(value) => actions.updateEntryDraft({ absent: value })} />
        <AttendanceEntryInput label="Total Tardy" value={entryDraft.tardy} placeholder="e.g. 1" onChange={(value) => actions.updateEntryDraft({ tardy: value })} />
        <AttendanceEntryInput label="Notes (optional)" type="text" value={entryDraft.notes} placeholder="Any notes..." onChange={(value) => actions.updateEntryDraft({ notes: value })} />
      </div>
      {draftPercentage !== null && (
        <div className="mt-4 p-3 rounded-xl bg-slate-700/30 border border-slate-600/30">
          <p className="text-sm text-slate-300">
            Calculated Attendance:{' '}
            <span className={`font-bold text-lg ${percentageTextClass(draftPercentage)}`}>
              {draftPercentage.toFixed(1)}%
            </span>
          </p>
        </div>
      )}
      {entryError && <p className="mt-3 text-sm text-red-300">{entryError}</p>}
      <div className="mt-4 flex justify-end">
        <Button
          type="button"
          onClick={actions.handleSubmitEntry}
          disabled={saving || !entryDraft.enrolled || !entryDraft.present || !entryDraft.absent}
          loading={saving}
          loadingLabel="Saving..."
          leadingIcon={<Save size={14} />}
          className="flex items-center gap-2 px-6 py-2.5 bg-gradient-to-r from-orange-500 to-amber-500 text-white rounded-xl font-medium text-sm hover:shadow-lg transition-all disabled:opacity-50"
        >
          Save Attendance
        </Button>
      </div>
    </div>
  );
}

type AttendanceEntryInputProps = {
  label: string;
  value: string;
  onChange: (value: string) => void;
  type?: 'date' | 'number' | 'text';
  placeholder?: string;
};

function AttendanceEntryInput({
  label,
  value,
  onChange,
  type = 'number',
  placeholder,
}: AttendanceEntryInputProps) {
  return (
    <div>
      <label className="block text-xs text-slate-400 mb-1.5 font-medium">{label}</label>
      <Input
        type={type}
        value={value}
        onChange={(event) => onChange(event.target.value)}
        placeholder={placeholder}
        className="w-full px-4 py-2.5 bg-slate-700/50 border border-slate-600/50 rounded-xl text-sm text-white focus:ring-2 focus:ring-orange-500/50 outline-none placeholder:text-slate-500"
      />
    </div>
  );
}
