import type { Calendar, CalendarEvent } from '@/lib/jmap/types';
import { demoDate, demoISODate } from '../demo-utils';

export function createDemoCalendars(): Calendar[] {
  return [
    {
      id: 'demo-calendar-personal',
      name: 'Personal',
      description: null,
      color: '#3b82f6',
      sortOrder: 1,
      isSubscribed: true,
      isVisible: true,
      isDefault: true,
      includeInAvailability: 'all',
      defaultAlertsWithTime: null,
      defaultAlertsWithoutTime: null,
      timeZone: null,
      shareWith: null,
      myRights: { mayReadFreeBusy: true, mayReadItems: true, mayWriteAll: true, mayWriteOwn: true, mayUpdatePrivate: true, mayRSVP: true, mayShare: true, mayDelete: false },
    },
    {
      id: 'demo-calendar-work',
      name: 'Work',
      description: null,
      color: '#22c55e',
      sortOrder: 2,
      isSubscribed: true,
      isVisible: true,
      isDefault: false,
      includeInAvailability: 'all',
      defaultAlertsWithTime: null,
      defaultAlertsWithoutTime: null,
      timeZone: null,
      shareWith: null,
      myRights: { mayReadFreeBusy: true, mayReadItems: true, mayWriteAll: true, mayWriteOwn: true, mayUpdatePrivate: true, mayRSVP: true, mayShare: true, mayDelete: true },
    },
    {
      id: 'demo-calendar-birthdays',
      name: 'Birthdays',
      description: null,
      color: '#eab308',
      sortOrder: 3,
      isSubscribed: true,
      isVisible: true,
      isDefault: false,
      includeInAvailability: 'none',
      defaultAlertsWithTime: null,
      defaultAlertsWithoutTime: null,
      timeZone: null,
      shareWith: null,
      myRights: { mayReadFreeBusy: true, mayReadItems: true, mayWriteAll: true, mayWriteOwn: true, mayUpdatePrivate: true, mayRSVP: true, mayShare: true, mayDelete: true },
    },
  ];
}

export function createDemoCalendarEvents(): CalendarEvent[] {
  const baseEvent = {
    '@type': 'Event' as const,
    descriptionContentType: 'text/plain',
    isDraft: false,
    isOrigin: true,
    sequence: 0,
    status: 'confirmed' as const,
    freeBusyStatus: 'busy' as const,
    privacy: 'public' as const,
    color: null,
    keywords: null,
    categories: null,
    locale: null,
    replyTo: null,
    organizerCalendarAddress: null,
    participants: null,
    mayInviteSelf: false,
    mayInviteOthers: false,
    hideAttendees: false,
    recurrenceId: null,
    recurrenceIdTimeZone: null,
    recurrenceRules: null,
    recurrenceOverrides: null,
    excludedRecurrenceRules: null,
    useDefaultAlerts: true,
    alerts: null,
    locations: null,
    virtualLocations: null,
    links: null,
    relatedTo: null,
  };

  return [
    // ── Personal calendar ──────────────────────────────────────
    {
      ...baseEvent,
      id: 'demo-event-1',
      calendarIds: { 'demo-calendar-personal': true },
      uid: 'demo-event-1@example.com',
      title: 'Dentist Appointment',
      description: 'Regular checkup at Dr. Smith\'s office',
      created: demoDate(-7),
      updated: demoDate(-7),
      start: demoISODate(2, 10, 0),
      utcStart: demoDate(2, 10),
      utcEnd: demoDate(2, 11),
      duration: 'PT1H',
      timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      showWithoutTime: false,
      locations: { loc1: { '@type': 'Location', name: 'Dr. Smith Dental Clinic', description: '123 Medical Plaza', locationTypes: null, coordinates: null, timeZone: null, links: null, relativeTo: null } },
    },
    {
      ...baseEvent,
      id: 'demo-event-2',
      calendarIds: { 'demo-calendar-personal': true },
      uid: 'demo-event-2@example.com',
      title: 'Birthday Party',
      description: 'Emma\'s birthday celebration',
      created: demoDate(-10),
      updated: demoDate(-10),
      start: demoISODate(5),
      utcStart: demoDate(5),
      utcEnd: demoDate(6),
      duration: 'P1D',
      timeZone: null,
      showWithoutTime: true,
      freeBusyStatus: 'free' as const,
    },
    {
      ...baseEvent,
      id: 'demo-event-3',
      calendarIds: { 'demo-calendar-personal': true },
      uid: 'demo-event-3@example.com',
      title: 'Weekend Trip',
      description: 'Road trip to the mountains',
      created: demoDate(-5),
      updated: demoDate(-5),
      start: demoISODate(8),
      utcStart: demoDate(8),
      utcEnd: demoDate(10),
      duration: 'P2D',
      timeZone: null,
      showWithoutTime: true,
      freeBusyStatus: 'busy' as const,
    },

    // ── Work calendar ──────────────────────────────────────────
    {
      ...baseEvent,
      id: 'demo-event-4',
      calendarIds: { 'demo-calendar-work': true },
      uid: 'demo-event-4@example.com',
      title: 'Weekly Standup',
      description: 'Team sync-up meeting',
      created: demoDate(-30),
      updated: demoDate(-1),
      start: demoISODate(1, 9, 30),
      utcStart: demoDate(1, 9, 30),
      utcEnd: demoDate(1, 10, 0),
      duration: 'PT30M',
      timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      showWithoutTime: false,
      recurrenceRules: [{
        '@type': 'RecurrenceRule',
        frequency: 'weekly',
        interval: 1,
        rscale: 'gregorian',
        skip: 'omit',
        firstDayOfWeek: 'mo',
        byDay: [{ day: 'mo' }],
        byMonthDay: null,
        byMonth: null,
        byYearDay: null,
        byWeekNo: null,
        byHour: null,
        byMinute: null,
        bySecond: null,
        bySetPosition: null,
        count: null,
        until: null,
      }],
      virtualLocations: { vl1: { '@type': 'VirtualLocation', name: 'Zoom', uri: 'https://zoom.example/123456', description: 'Weekly standup room', features: null } },
    },
    {
      ...baseEvent,
      id: 'demo-event-5',
      calendarIds: { 'demo-calendar-work': true },
      uid: 'demo-event-5@example.com',
      title: 'Quarterly Review',
      description: 'Q4 performance review and planning session',
      created: demoDate(-14),
      updated: demoDate(-3),
      start: demoISODate(4, 14, 0),
      utcStart: demoDate(4, 14),
      utcEnd: demoDate(4, 16),
      duration: 'PT2H',
      timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      showWithoutTime: false,
      participants: {
        p1: {
          '@type': 'Participant', name: 'Demo User', email: 'demo@example.com', calendarAddress: null, description: null, sendTo: null,
          kind: 'individual', roles: { attendee: true }, participationStatus: 'accepted', participationComment: null,
          expectReply: false, scheduleAgent: 'server', scheduleForceSend: false, scheduleId: null, scheduleSequence: 0,
          scheduleStatus: null, scheduleUpdated: null, invitedBy: null, delegatedTo: null, delegatedFrom: null, memberOf: null,
          locationId: null, language: null, links: null,
        },
        p2: {
          '@type': 'Participant', name: 'Alice Johnson', email: 'alice.johnson@example.com', calendarAddress: null, description: null, sendTo: null,
          kind: 'individual', roles: { owner: true }, participationStatus: 'accepted', participationComment: null,
          expectReply: false,  scheduleAgent: 'server', scheduleForceSend: false, scheduleId: null, scheduleSequence: 0,
          scheduleStatus: null, scheduleUpdated: null, invitedBy: null, delegatedTo: null, delegatedFrom: null, memberOf: null,
          locationId: null, language: null, links: null,
        },
        p3: {
          '@type': 'Participant', name: 'Bob Chen', email: 'bob.chen@example.com', calendarAddress: null, description: null, sendTo: null,
          kind: 'individual', roles: { attendee: true }, participationStatus: 'tentative', participationComment: null,
          expectReply: true, scheduleAgent: 'server', scheduleForceSend: false, scheduleId: null, scheduleSequence: 0,
          scheduleStatus: null, scheduleUpdated: null, invitedBy: null, delegatedTo: null, delegatedFrom: null, memberOf: null,
          locationId: null, language: null, links: null,
        },
      },
    },
    {
      ...baseEvent,
      id: 'demo-event-6',
      calendarIds: { 'demo-calendar-work': true },
      uid: 'demo-event-6@example.com',
      title: 'Lunch Meeting with Sarah',
      description: 'Design review over lunch',
      created: demoDate(-3),
      updated: demoDate(-3),
      start: demoISODate(3, 12, 0),
      utcStart: demoDate(3, 12),
      utcEnd: demoDate(3, 13),
      duration: 'PT1H',
      timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      showWithoutTime: false,
      locations: { loc1: { '@type': 'Location', name: 'The Garden Bistro', description: '123 Oak Street', locationTypes: null, coordinates: null, timeZone: null, links: null, relativeTo: null } },
    },

    // ── Birthdays calendar ─────────────────────────────────────
    {
      ...baseEvent,
      id: 'demo-event-7',
      calendarIds: { 'demo-calendar-birthdays': true },
      uid: 'demo-event-7@example.com',
      title: 'Alice Johnson\'s Birthday',
      description: '',
      created: demoDate(-30),
      updated: demoDate(-30),
      start: demoISODate(12),
      utcStart: demoDate(12),
      utcEnd: demoDate(13),
      duration: 'P1D',
      timeZone: null,
      showWithoutTime: true,
      freeBusyStatus: 'free' as const,
    },
    {
      ...baseEvent,
      id: 'demo-event-8',
      calendarIds: { 'demo-calendar-birthdays': true },
      uid: 'demo-event-8@example.com',
      title: 'Carlos Rivera\'s Birthday',
      description: '',
      created: demoDate(-30),
      updated: demoDate(-30),
      start: demoISODate(-3),
      utcStart: demoDate(-3),
      utcEnd: demoDate(-2),
      duration: 'P1D',
      timeZone: null,
      showWithoutTime: true,
      freeBusyStatus: 'free' as const,
    },
  ];
}
