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

// Query for paginated reports with filtering
export const REPORTS_QUERY = gql`
	query Reports($filter: ReportFilterInput, $pagination: PaginationInput) {
		reports(filter: $filter, pagination: $pagination) {
			items {
				id
				teamProfileId
				startDate
				endDate
				status
				teamProfile {
					id
					fullName
					role
				}
				servicesTotal
				projectsTotal
				totalLabor
				serviceCount
				projectCount
			}
			totalCount
			hasNextPage
		}
	}
`;

// Query for a single report with full details
export const REPORT_QUERY = gql`
	query Report($id: UUID!) {
		report(id: $id) {
			id
			teamProfileId
			startDate
			endDate
			status
			teamProfile {
				id
				fullName
				role
			}
			services {
				id
				serviceId
				laborShare
				service {
					id
					date
					account {
						id
						name
						customer {
							id
							name
						}
					}
					accountAddress {
						id
						name
						streetAddress
						city
					}
				}
			}
			projects {
				id
				projectId
				laborShare
				project {
					id
					name
					date
					customer {
						id
						name
					}
					formattedAddress
				}
			}
			servicesTotal
			projectsTotal
			totalLabor
			serviceCount
			projectCount
		}
	}
`;

// Query for eligible services to add to a report
export const ELIGIBLE_SERVICES_QUERY = gql`
	query EligibleServicesForReport(
		$teamProfileId: UUID!
		$dateFrom: NaiveDate!
		$dateTo: NaiveDate!
	) {
		eligibleServicesForReport(teamProfileId: $teamProfileId, dateFrom: $dateFrom, dateTo: $dateTo) {
			serviceId
			date
			laborShare
			laborTotal
			teamMemberCount
			service {
				id
				date
				account {
					id
					name
					customer {
						id
						name
					}
				}
				accountAddress {
					id
					name
					streetAddress
					city
				}
			}
		}
	}
`;

// Query for eligible projects to add to a report
export const ELIGIBLE_PROJECTS_QUERY = gql`
	query EligibleProjectsForReport(
		$teamProfileId: UUID!
		$dateFrom: NaiveDate!
		$dateTo: NaiveDate!
	) {
		eligibleProjectsForReport(teamProfileId: $teamProfileId, dateFrom: $dateFrom, dateTo: $dateTo) {
			projectId
			date
			laborShare
			laborTotal
			teamMemberCount
			project {
				id
				name
				date
				customer {
					id
					name
				}
				formattedAddress
			}
		}
	}
`;

// Types
export type ReportStatus = 'DRAFT' | 'FINALIZED' | 'PAID';

export interface ReportTeamProfile {
	id: string;
	fullName: string;
	role: 'ADMIN' | 'TEAM_LEADER' | 'TEAM_MEMBER';
}

export interface ReportServiceEntry {
	id: string;
	serviceId: string;
	laborShare: number;
	service: {
		id: string;
		date: string;
		account: {
			id: string;
			name: string;
			customer: {
				id: string;
				name: string;
			} | null;
		} | null;
		accountAddress: {
			id: string;
			name: string | null;
			streetAddress: string;
			city: string;
		} | null;
	} | null;
}

export interface ReportProjectEntry {
	id: string;
	projectId: string;
	laborShare: number;
	project: {
		id: string;
		name: string;
		date: string;
		customer: {
			id: string;
			name: string;
		} | null;
		formattedAddress: string | null;
	} | null;
}

export interface ReportListItem {
	id: string;
	teamProfileId: string;
	startDate: string;
	endDate: string;
	status: ReportStatus;
	teamProfile: ReportTeamProfile | null;
	servicesTotal: number;
	projectsTotal: number;
	totalLabor: number;
	serviceCount: number;
	projectCount: number;
}

export interface ReportDetail {
	id: string;
	teamProfileId: string;
	startDate: string;
	endDate: string;
	status: ReportStatus;
	teamProfile: ReportTeamProfile | null;
	services: ReportServiceEntry[];
	projects: ReportProjectEntry[];
	servicesTotal: number;
	projectsTotal: number;
	totalLabor: number;
	serviceCount: number;
	projectCount: number;
}

export interface ReportsQueryResult {
	reports: {
		items: ReportListItem[];
		totalCount: number;
		hasNextPage: boolean;
	};
}

export interface ReportQueryResult {
	report: ReportDetail | null;
}

export interface EligibleService {
	serviceId: string;
	date: string;
	laborShare: number;
	laborTotal: number;
	teamMemberCount: number;
	service: {
		id: string;
		date: string;
		account: {
			id: string;
			name: string;
			customer: {
				id: string;
				name: string;
			} | null;
		} | null;
		accountAddress: {
			id: string;
			name: string | null;
			streetAddress: string;
			city: string;
		} | null;
	} | null;
}

export interface EligibleProject {
	projectId: string;
	date: string;
	laborShare: number;
	laborTotal: number;
	teamMemberCount: number;
	project: {
		id: string;
		name: string;
		date: string;
		customer: {
			id: string;
			name: string;
		} | null;
		formattedAddress: string | null;
	} | null;
}

export interface EligibleServicesQueryResult {
	eligibleServicesForReport: EligibleService[];
}

export interface EligibleProjectsQueryResult {
	eligibleProjectsForReport: EligibleProject[];
}

export interface ReportFilterInput {
	teamProfileId?: string;
	status?: ReportStatus;
	date?: string;
	startDateFrom?: string;
	endDateTo?: string;
}
