export type Priority = 'none' | 'low' | 'medium' | 'high' | 'critical';

export type Repeat = 'none' | 'daily' | 'weekly' | 'monthly' | 'custom';

export const REPEAT_OPTIONS: { value: Repeat; label: string }[] = [
  { value: 'none', label: 'No Repeat' },
  { value: 'daily', label: 'Daily' },
  { value: 'weekly', label: 'Weekly' },
  { value: 'monthly', label: 'Monthly' },
  { value: 'custom', label: 'Custom' },
];

export const WEEKDAY_LABELS = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];

export const WEEKDAY_ORDER = [1, 2, 3, 4, 5, 6, 0];

export function weekdaysInDisplayOrder(days: number[]): number[] {
  return WEEKDAY_ORDER.filter((day) => days.includes(day));
}

export function repeatDaysToString(days: number[]): string {
  return [...days].sort().join(',');
}

export function repeatDaysFromString(raw: string): number[] {
  return (raw || '')
    .split(',')
    .map(Number)
    .filter((d) => !Number.isNaN(d));
}

export interface RepeatProfileData {
  id: string;
  name: string;
  repeat: Repeat;
  repeatInterval: number;
  repeatDays: string;
  createdAt: Date;
  updatedAt: Date;
}

export type Reminder = 'none' | 'at_time' | '15' | '30' | '60' | '120' | '1440';

export const REMINDER_OPTIONS: { value: Reminder; label: string }[] = [
  { value: 'none', label: 'No Reminder' },
  { value: 'at_time', label: 'At due time' },
  { value: '15', label: '15 minutes before' },
  { value: '30', label: '30 minutes before' },
  { value: '60', label: '1 hour before' },
  { value: '120', label: '2 hours before' },
  { value: '1440', label: '1 day before' },
];

export const MULTIPLE_REMINDER_OPTIONS = REMINDER_OPTIONS.filter((o) => o.value !== 'none');

export function reminderToMinutes(reminder: Reminder): number | null {
  if (reminder === 'none') return null;
  if (reminder === 'at_time') return 0;
  return parseInt(reminder, 10);
}

export function reminderShortLabel(reminder: Reminder): string {
  switch (reminder) {
    case 'none':
      return '';
    case 'at_time':
      return 'at due time';
    case '15':
      return '15 min';
    case '30':
      return '30 min';
    case '60':
      return '1 hr';
    case '120':
      return '2 hrs';
    case '1440':
      return '1 day';
    default:
      return '';
  }
}

export function parseReminders(reminders: string): Reminder[] {
  if (!reminders) return [];
  return reminders
    .split(',')
    .map((r) => r.trim() as Reminder)
    .filter((r): r is Exclude<Reminder, 'none'> => r !== 'none' && Boolean(r));
}

export function toRemindersString(reminders: Reminder[]): string {
  return reminders.filter((r) => r !== 'none').join(',');
}

export interface CategoryData {
  id: string;
  name: string;
  color: string;
  order: number;
}

export function tagsToString(tags: string[]): string {
  return `,${tags.filter(Boolean).join(',')},`;
}

export function tagsFromString(raw: string | null | undefined): string[] {
  if (!raw) return [];
  return raw
    .split(',')
    .map((t) => t.trim())
    .filter(Boolean);
}

export function parseTaskTags(tagsRaw: string | null | undefined, categoryId: string): string[] {
  const parsed = tagsFromString(tagsRaw);
  if (parsed.length > 0) return parsed;
  return categoryId ? [categoryId] : [];
}

export interface TaskData {
  id: string;
  title: string;
  description: string;
  categoryId: string;
  tags?: string;
  priority: Priority;
  completed: boolean;
  dueDate: number;
  dueTime: string;
  endTime: string;
  allDay: boolean;
  repeat: Repeat;
  repeatInterval: number;
  repeatDays: string;
  reminder: Reminder;
  reminders: string;
  assigneeId: string | null;
  createdAt: Date;
  updatedAt: Date;
  subtasks: SubtaskData[];
}

export interface SubtaskData {
  id: string;
  taskId: string;
  parentSubtaskId: string | null;
  categoryId: string;
  tags: string;
  title: string;
  description: string;
  priority: Priority;
  completed: boolean;
  dueDate: number;
  dueTime: string;
  endTime: string;
  allDay: boolean;
  repeat: Repeat;
  repeatInterval: number;
  repeatDays: string;
  seriesId: string;
  reminder: Reminder;
  assigneeId: string | null;
  order: number;
  subtasks: SubtaskData[];
}

export interface SubtaskFormValue {
  title: string;
  _key?: string;
  parentSubtaskId?: string | null;
  subtasks?: SubtaskFormValue[];
}

export interface TaskFormData {
  title: string;
  description?: string;
  categoryId: string;
  tags?: string[];
  priority: Priority;
  dueDate: Date | null;
  dueTime?: string;
  endTime?: string;
  allDay?: boolean;
  repeat: Repeat;
  repeatInterval?: number;
  repeatDays?: number[];
  reminder: Reminder;
  reminders: string;
  assigneeId: string | null;
  subtasks?: SubtaskFormValue[];
}

export interface SubtaskFormData {
  title: string;
  description?: string;
  categoryId?: string;
  priority: Priority;
  dueDate: Date | null;
  dueTime?: string;
  endTime?: string;
  allDay?: boolean;
  repeat: Repeat;
  repeatInterval?: number;
  repeatDays?: number[];
  reminder: Reminder;
  reminders: string;
  assigneeId: string | null;
  parentSubtaskId?: string | null;
}