import { gql } from '@apollo/client/core';

export const CALENDAR_EVENTS_QUERY = gql`
	query CalendarEvents($filter: CalendarEventFilterInput) {
		calendarEvents(filter: $filter) {
			id
			summary
			description
			location
			start {
				dateTime
				date
				timeZone
			}
			end {
				dateTime
				date
				timeZone
			}
			attendees {
				email
				displayName
				optional
				responseStatus
			}
			colorId
			htmlLink
			created
			updated
			status
		}
	}
`;

export const CALENDAR_EVENT_QUERY = gql`
	query CalendarEvent($eventId: String!) {
		calendarEvent(eventId: $eventId) {
			id
			summary
			description
			location
			start {
				dateTime
				date
				timeZone
			}
			end {
				dateTime
				date
				timeZone
			}
			attendees {
				email
				displayName
				optional
				responseStatus
			}
			reminders {
				useDefault
				overrides {
					method
					minutes
				}
			}
			colorId
			htmlLink
			created
			updated
			status
		}
	}
`;

export const TODAYS_EVENTS_QUERY = gql`
	query TodaysEvents {
		todaysEvents {
			id
			summary
			description
			location
			start {
				dateTime
				date
				timeZone
			}
			end {
				dateTime
				date
				timeZone
			}
			colorId
			status
		}
	}
`;

export const UPCOMING_EVENTS_QUERY = gql`
	query UpcomingEvents($days: Int) {
		upcomingEvents(days: $days) {
			id
			summary
			description
			location
			start {
				dateTime
				date
				timeZone
			}
			end {
				dateTime
				date
				timeZone
			}
			colorId
			status
		}
	}
`;

// Types
export interface EventDateTime {
	dateTime: string | null;
	date: string | null;
	timeZone: string | null;
}

export interface Attendee {
	email: string;
	displayName: string | null;
	optional: boolean | null;
	responseStatus: string | null;
}

export interface EventReminder {
	method: string;
	minutes: number;
}

export interface EventReminders {
	useDefault: boolean;
	overrides: EventReminder[] | null;
}

export interface CalendarEvent {
	id: string;
	summary: string;
	description: string | null;
	location: string | null;
	start: EventDateTime;
	end: EventDateTime;
	attendees: Attendee[] | null;
	reminders?: EventReminders | null;
	colorId: string | null;
	htmlLink: string | null;
	created: string | null;
	updated: string | null;
	status: string | null;
}

export interface CalendarEventFilterInput {
	timeMin?: string;
	timeMax?: string;
	maxResults?: number;
	q?: string;
}

export interface CalendarEventsQueryResult {
	calendarEvents: CalendarEvent[];
}

export interface CalendarEventQueryResult {
	calendarEvent: CalendarEvent | null;
}

export interface TodaysEventsQueryResult {
	todaysEvents: CalendarEvent[];
}

export interface UpcomingEventsQueryResult {
	upcomingEvents: CalendarEvent[];
}

// Google Calendar color IDs and their display values
export const EVENT_COLORS = [
	{ id: '1', name: 'Lavender', bg: 'bg-purple-300', text: 'text-purple-900' },
	{ id: '2', name: 'Sage', bg: 'bg-green-300', text: 'text-green-900' },
	{ id: '3', name: 'Grape', bg: 'bg-violet-400', text: 'text-violet-900' },
	{ id: '4', name: 'Flamingo', bg: 'bg-pink-400', text: 'text-pink-900' },
	{ id: '5', name: 'Banana', bg: 'bg-yellow-300', text: 'text-yellow-900' },
	{ id: '6', name: 'Tangerine', bg: 'bg-orange-400', text: 'text-orange-900' },
	{ id: '7', name: 'Peacock', bg: 'bg-cyan-500', text: 'text-cyan-900' },
	{ id: '8', name: 'Graphite', bg: 'bg-gray-400', text: 'text-gray-900' },
	{ id: '9', name: 'Blueberry', bg: 'bg-blue-500', text: 'text-blue-100' },
	{ id: '10', name: 'Basil', bg: 'bg-emerald-600', text: 'text-emerald-100' },
	{ id: '11', name: 'Tomato', bg: 'bg-red-500', text: 'text-red-100' }
] as const;

export function getEventColor(colorId: string | null) {
	return EVENT_COLORS.find((c) => c.id === colorId) ?? null;
}
