interface ScheduleState {
  scheduleType: string;
  hour: number;
  minute: number;
  dayOfWeek: number;
  dayOfMonth: number;
  cronExpression: string;
  scheduledAt: string;
}

interface ScheduleFieldsProps {
  schedule: ScheduleState;
  onChange: (updater: (prev: ScheduleState) => ScheduleState) => void;
}

export function ScheduleFields({ schedule, onChange }: ScheduleFieldsProps) {
  return (
    <div className="pl-4 border-l-2 border-blue-200 space-y-2 mt-2">
      <div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
        <div>
          <label className="block text-xs text-slate-600 mb-1">タイプ</label>
          <select
            value={schedule.scheduleType}
            onChange={e => onChange(p => ({ ...p, scheduleType: e.target.value }))}
            className="w-full px-2 py-1.5 border border-slate-200 rounded-lg text-xs"
          >
            <option value="daily">毎日</option>
            <option value="weekly">毎週</option>
            <option value="monthly">毎月</option>
            <option value="cron">Cron式</option>
            <option value="once">一回のみ</option>
          </select>
        </div>
        {schedule.scheduleType !== 'cron' && schedule.scheduleType !== 'once' && (
          <div>
            <label className="block text-xs text-slate-600 mb-1">時刻</label>
            <div className="flex items-center gap-1">
              <input
                type="number"
                min={0}
                max={23}
                value={schedule.hour}
                onChange={e => onChange(p => ({ ...p, hour: Number(e.target.value) }))}
                className="w-14 px-1 py-1.5 border border-slate-200 rounded-lg text-xs text-center"
              />
              <span>:</span>
              <input
                type="number"
                min={0}
                max={59}
                value={schedule.minute}
                onChange={e => onChange(p => ({ ...p, minute: Number(e.target.value) }))}
                className="w-14 px-1 py-1.5 border border-slate-200 rounded-lg text-xs text-center"
              />
            </div>
          </div>
        )}
      </div>
      {schedule.scheduleType === 'weekly' && (
        <div>
          <label className="block text-xs text-slate-600 mb-1">曜日</label>
          <select
            value={schedule.dayOfWeek}
            onChange={e => onChange(p => ({ ...p, dayOfWeek: Number(e.target.value) }))}
            className="w-full px-2 py-1.5 border border-slate-200 rounded-lg text-xs"
          >
            {['日曜', '月曜', '火曜', '水曜', '木曜', '金曜', '土曜'].map((d, i) => (
              <option key={i} value={i}>{d}</option>
            ))}
          </select>
        </div>
      )}
      {schedule.scheduleType === 'monthly' && (
        <div>
          <label className="block text-xs text-slate-600 mb-1">日</label>
          <input
            type="number"
            min={1}
            max={31}
            value={schedule.dayOfMonth}
            onChange={e => onChange(p => ({ ...p, dayOfMonth: Number(e.target.value) }))}
            className="w-20 px-2 py-1.5 border border-slate-200 rounded-lg text-xs"
          />
        </div>
      )}
      {schedule.scheduleType === 'cron' && (
        <div>
          <label className="block text-xs text-slate-600 mb-1">Cron式</label>
          <input
            value={schedule.cronExpression}
            onChange={e => onChange(p => ({ ...p, cronExpression: e.target.value }))}
            className="w-full px-2 py-1.5 border border-slate-200 rounded-lg text-xs font-mono"
            placeholder="0 9 * * MON"
          />
        </div>
      )}
      {schedule.scheduleType === 'once' && (
        <div>
          <label className="block text-xs text-slate-600 mb-1">実行日時</label>
          <input
            type="datetime-local"
            value={schedule.scheduledAt}
            onChange={e => onChange(p => ({ ...p, scheduledAt: e.target.value }))}
            className="w-full px-2 py-1.5 border border-slate-200 rounded-lg text-xs"
          />
        </div>
      )}
    </div>
  );
}
