export interface User {
  id: string;
  email: string;
  timezone: string;
  week_start_day?: number;
  date_format?: string;
  time_format?: string;
  default_event_duration_minutes?: number;
  default_reminder_minutes?: number;
  show_weekends?: boolean;
  working_hours_start?: string;
  working_hours_end?: string;
  notifications_email?: boolean;
  created_at: string;
  updated_at: string;
}

export interface AuthResponse {
  user: User;
  access_token: string;
  refresh_token: string;
}

export interface Calendar {
  id: string;
  name: string;
  color: string;
  is_public: boolean;
  count_for_availability?: boolean;
  default_reminder_minutes?: number;
  sort_order?: number;
  ical_url?: string;
  availability_url?: string;
  role: "owner" | "editor" | "viewer";
  created_at: string;
  updated_at: string;
}

export interface CalendarSubscription {
  id: string;
  calendar_id: string;
  source_url: string;
  last_synced_at?: string;
  sync_interval_minutes?: number;
  created_at: string;
}

export interface Reminder {
  id: string;
  minutes_before: number;
}

export interface Attendee {
  id: string;
  user_id?: string;
  email?: string;
  status: "pending" | "accepted" | "declined" | "tentative";
}

export interface CalendarEvent {
  id: string;
  calendar_id: string;
  title: string;
  description?: string;
  location?: string;
  start_time: string;
  end_time: string;
  timezone: string;
  all_day: boolean;
  recurrence_rule?: string;
  is_occurrence?: boolean;
  occurrence_start_time?: string;
  occurrence_end_time?: string;
  created_by: string;
  updated_by: string;
  created_at: string;
  updated_at: string;
  reminders?: Reminder[];
  attendees?: Attendee[];
  tags?: string[];
}

export interface Contact {
  id: string;
  first_name?: string;
  last_name?: string;
  email?: string;
  phone?: string;
  company?: string;
  notes?: string;
  created_at: string;
  updated_at: string;
}

export interface CalendarMember {
  calendar_id: string;
  user_id: string;
  email: string;
  role: "owner" | "editor" | "viewer";
}

export interface PageInfo {
  limit: number;
  next_cursor?: string;
}

export interface ListResponse<T> {
  items: T[];
  page: PageInfo;
}

export interface APIError {
  code: string;
  error: string;
  details?: string;
}

export type ViewMode = "month" | "week" | "day";

/** Get effective start/end for an event (use occurrence times for recurring instances). */
export function eventStart(e: CalendarEvent): string {
  return e.is_occurrence && e.occurrence_start_time ? e.occurrence_start_time : e.start_time;
}
export function eventEnd(e: CalendarEvent): string {
  return e.is_occurrence && e.occurrence_end_time ? e.occurrence_end_time : e.end_time;
}

export interface APIKey {
  id: string;
  name: string;
  created_at: string;
  revoked_at?: string;
  token?: string; // Only present on creation
}

export type APIKeyScopes = {
  calendars?: ("read" | "write")[];
  events?: ("read" | "write")[];
  contacts?: ("read" | "write")[];
  availability?: ("read")[];
  booking?: ("write")[];
};
