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

export const PROJECTS_QUERY = gql`
	query Projects($filter: ProjectFilterInput, $pagination: PaginationInput) {
		projects(filter: $filter, pagination: $pagination) {
			items {
				id
				customerId
				name
				date
				status
				labor
				amount
				notes
				calendarEventId
				accountAddressId
				streetAddress
				city
				state
				zipCode
				formattedAddress
				customer {
					id
					name
				}
				accountAddress {
					id
					name
					streetAddress
					city
					state
					zipCode
					account {
						id
						name
					}
				}
				teamMembers {
					id
					teamProfileId
					teamProfile {
						id
						fullName
						role
					}
				}
			}
			totalCount
			hasNextPage
		}
	}
`;

export const PROJECT_QUERY = gql`
	query Project($id: UUID!) {
		project(id: $id) {
			id
			customerId
			name
			date
			status
			labor
			amount
			notes
			calendarEventId
			waveServiceId
			waveProductName
			accountAddressId
			streetAddress
			city
			state
			zipCode
			formattedAddress
			customer {
				id
				name
			}
			accountAddress {
				id
				name
				streetAddress
				city
				state
				zipCode
				account {
					id
					name
				}
			}
			teamMembers {
				id
				teamProfileId
				teamProfile {
					id
					fullName
					role
				}
			}
			scopes {
				id
				name
				description
				isActive
				categories {
					id
					name
					order
					tasks {
						id
						scopeDescription
						checklistDescription
						sessionDescription
						order
						estimatedMinutes
					}
				}
			}
		}
	}
`;

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

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

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

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

export interface ProjectCustomer {
	id: string;
	name: string;
}

export interface ProjectAddressAccount {
	id: string;
	name: string;
}

export interface ProjectAddress {
	id: string;
	name: string | null;
	streetAddress: string;
	city: string;
	state: string;
	zipCode: string;
	account: ProjectAddressAccount;
}

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

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

export interface ProjectTeamMember {
	id: string;
	teamProfileId: string;
	teamProfile: ProjectTeamMemberProfile | null;
}

export interface ProjectListItem {
	id: string;
	customerId: string;
	name: string;
	date: string;
	status: WorkStatus;
	labor: number | null;
	amount: number | null;
	notes: string | null;
	calendarEventId: string | null;
	accountAddressId: string | null;
	streetAddress: string | null;
	city: string | null;
	state: string | null;
	zipCode: string | null;
	formattedAddress: string | null;
	customer: ProjectCustomer | null;
	accountAddress: ProjectAddress | null;
	teamMembers: ProjectTeamMember[];
}

export interface ProjectConnection {
	items: ProjectListItem[];
	totalCount: number;
	hasNextPage: boolean;
}

export interface ProjectsQueryResult {
	projects: ProjectConnection;
}

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

export interface ProjectStatusCountsQueryResult {
	projectStatusCounts: ProjectStatusCounts;
}

export interface ProjectScopeTask {
	id: string;
	scopeDescription: string;
	checklistDescription: string;
	sessionDescription: string;
	order: number;
	estimatedMinutes: number | null;
}

export interface ProjectScopeCategory {
	id: string;
	name: string;
	order: number;
	tasks: ProjectScopeTask[];
}

export interface ProjectScope {
	id: string;
	name: string;
	description: string | null;
	isActive: boolean;
	categories: ProjectScopeCategory[];
}

export interface ProjectDetail {
	id: string;
	customerId: string;
	name: string;
	date: string;
	status: WorkStatus;
	labor: number | null;
	amount: number | null;
	notes: string | null;
	calendarEventId: string | null;
	waveServiceId: string | null;
	waveProductName: string | null;
	accountAddressId: string | null;
	streetAddress: string | null;
	city: string | null;
	state: string | null;
	zipCode: string | null;
	formattedAddress: string | null;
	customer: ProjectCustomer | null;
	accountAddress: ProjectAddress | null;
	teamMembers: ProjectTeamMember[];
	scopes: ProjectScope[];
}

export interface ProjectQueryResult {
	project: ProjectDetail | null;
}
