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

export const SERVICES_QUERY = gql`
	query Services($filter: ServiceFilterInput, $pagination: PaginationInput) {
		services(filter: $filter, pagination: $pagination) {
			items {
				id
				accountId
				accountAddressId
				date
				status
				notes
				calendarEventId
				account {
					id
					name
					customer {
						id
						name
					}
				}
				accountAddress {
					id
					name
					streetAddress
					city
					state
					zipCode
				}
				teamMembers {
					id
					teamProfileId
					teamProfile {
						id
						fullName
						role
					}
				}
			}
			totalCount
			hasNextPage
		}
	}
`;

export const SERVICE_QUERY = gql`
	query Service($id: UUID!) {
		service(id: $id) {
			id
			accountId
			accountAddressId
			date
			status
			notes
			calendarEventId
			account {
				id
				name
				status
				customer {
					id
					name
				}
			}
			accountAddress {
				id
				name
				streetAddress
				city
				state
				zipCode
				labor {
					id
					amount
					startDate
					endDate
					isActive
				}
				activeScope {
					id
					name
					description
					isActive
					areas {
						id
						name
						order
						tasks {
							id
							scopeDescription
							sessionDescription
							frequency
							order
						}
					}
				}
			}
			teamMembers {
				id
				teamProfileId
				teamProfile {
					id
					fullName
					role
				}
			}
		}
	}
`;

export const SERVICE_STATUS_COUNTS_QUERY = gql`
	query ServiceStatusCounts(
		$dateFrom: NaiveDate
		$dateTo: NaiveDate
		$teamProfileId: UUID
		$customerIds: [UUID!]
	) {
		serviceStatusCounts(
			dateFrom: $dateFrom
			dateTo: $dateTo
			teamProfileId: $teamProfileId
			customerIds: $customerIds
		) {
			scheduled
			inProgress
			completed
			cancelled
		}
	}
`;

export type WorkStatus = 'SCHEDULED' | 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED';

export interface ServiceFilterInput {
	dateFrom?: string;
	dateTo?: string;
	status?: WorkStatus;
	accountId?: string;
	accountAddressId?: string;
	teamProfileId?: string;
	customerId?: string;
	customerIds?: string[];
}

export interface PaginationInput {
	offset?: number;
	limit?: number;
}

export interface ServiceAccount {
	id: string;
	name: string;
	customer: {
		id: string;
		name: string;
	} | null;
}

export interface ServiceAddress {
	id: string;
	name: string | null;
	streetAddress: string;
	city: string;
	state: string;
	zipCode: string;
}

export type TeamRole = 'ADMIN' | 'TEAM_LEADER' | 'TEAM_MEMBER';

export interface ServiceTeamMemberProfile {
	id: string;
	fullName: string;
	role: TeamRole;
}

export interface ServiceTeamMember {
	id: string;
	teamProfileId: string;
	teamProfile: ServiceTeamMemberProfile | null;
}

export interface ServiceListItem {
	id: string;
	accountId: string;
	accountAddressId: string;
	date: string;
	status: WorkStatus;
	notes: string | null;
	calendarEventId: string | null;
	account: ServiceAccount | null;
	accountAddress: ServiceAddress | null;
	teamMembers: ServiceTeamMember[];
}

export interface ServiceConnection {
	items: ServiceListItem[];
	totalCount: number;
	hasNextPage: boolean;
}

export interface ServicesQueryResult {
	services: ServiceConnection;
}

export interface ServiceStatusCounts {
	scheduled: number;
	inProgress: number;
	completed: number;
	cancelled: number;
}

export interface ServiceStatusCountsQueryResult {
	serviceStatusCounts: ServiceStatusCounts;
}

export interface ServiceLabor {
	id: string;
	amount: string;
	startDate: string;
	endDate: string | null;
	isActive: boolean;
}

export type TaskFrequency =
	| 'DAILY'
	| 'WEEKLY'
	| 'MONTHLY'
	| 'QUARTERLY'
	| 'TRIANNUAL'
	| 'ANNUAL'
	| 'AS_NEEDED';

export interface ServiceScopeTask {
	id: string;
	scopeDescription: string;
	sessionDescription: string;
	frequency: TaskFrequency;
	order: number;
}

export interface ServiceScopeArea {
	id: string;
	name: string;
	order: number;
	tasks: ServiceScopeTask[];
}

export interface ServiceScope {
	id: string;
	name: string;
	description: string | null;
	isActive: boolean;
	areas: ServiceScopeArea[];
}

export interface ServiceDetail {
	id: string;
	accountId: string;
	accountAddressId: string;
	date: string;
	status: WorkStatus;
	notes: string | null;
	calendarEventId: string | null;
	account: {
		id: string;
		name: string;
		status: string;
		customer: {
			id: string;
			name: string;
		} | null;
	} | null;
	accountAddress: {
		id: string;
		name: string | null;
		streetAddress: string;
		city: string;
		state: string;
		zipCode: string;
		labor: ServiceLabor[];
		activeScope: ServiceScope | null;
	} | null;
	teamMembers: ServiceTeamMember[];
}

export interface ServiceQueryResult {
	service: ServiceDetail | null;
}

// ==================== SERVICES FOR ASSIGNMENT QUERY ====================

export const SERVICES_FOR_ASSIGNMENT_QUERY = gql`
	query ServicesForAssignment($dateFrom: NaiveDate!, $dateTo: NaiveDate!) {
		servicesForAssignment(dateFrom: $dateFrom, dateTo: $dateTo) {
			unassigned {
				id
				date
				status
				account {
					id
					name
				}
				accountAddress {
					id
					name
					city
					state
				}
				teamMembers {
					id
					teamProfileId
					teamProfile {
						id
						fullName
						role
					}
				}
			}
			readyToAssign {
				id
				date
				status
				account {
					id
					name
				}
				accountAddress {
					id
					name
					city
					state
				}
				teamMembers {
					id
					teamProfileId
					teamProfile {
						id
						fullName
						role
					}
				}
			}
			assigned {
				id
				date
				status
				account {
					id
					name
				}
				accountAddress {
					id
					name
					city
					state
				}
				teamMembers {
					id
					teamProfileId
					teamProfile {
						id
						fullName
						role
					}
				}
			}
		}
	}
`;

export interface ServiceForAssignment {
	id: string;
	date: string;
	status: WorkStatus;
	account: {
		id: string;
		name: string;
	} | null;
	accountAddress: {
		id: string;
		name: string | null;
		city: string;
		state: string;
	} | null;
	teamMembers: ServiceTeamMember[];
}

export interface ServicesForAssignmentResult {
	unassigned: ServiceForAssignment[];
	readyToAssign: ServiceForAssignment[];
	assigned: ServiceForAssignment[];
}

export interface ServicesForAssignmentQueryResult {
	servicesForAssignment: ServicesForAssignmentResult;
}
