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

// Query for paginated invoices with filtering
export const INVOICES_QUERY = gql`
	query Invoices($filter: InvoiceFilterInput, $pagination: PaginationInput) {
		invoices(filter: $filter, pagination: $pagination) {
			items {
				id
				customerId
				startDate
				endDate
				status
				datePaid
				waveInvoiceId
				customer {
					id
					name
				}
				revenuesTotal
				projectsTotal
				totalAmount
				revenueCount
				projectCount
			}
			totalCount
			hasNextPage
		}
	}
`;

// Query for a single invoice with full details
export const INVOICE_QUERY = gql`
	query Invoice($id: UUID!) {
		invoice(id: $id) {
			id
			customerId
			startDate
			endDate
			status
			datePaid
			waveInvoiceId
			customer {
				id
				name
			}
			revenues {
				id
				revenueId
				amount
				account {
					id
					name
				}
				revenue {
					id
					amount
					startDate
					endDate
					waveServiceId
					isActive
				}
			}
			projects {
				id
				projectId
				amount
				account {
					id
					name
				}
				project {
					id
					name
					date
					amount
					formattedAddress
				}
			}
			revenuesTotal
			projectsTotal
			totalAmount
			revenueCount
			projectCount
		}
	}
`;

// Query for eligible revenues to add to an invoice
export const ELIGIBLE_REVENUES_QUERY = gql`
	query EligibleRevenuesForInvoice($customerId: UUID!, $dateFrom: NaiveDate!, $dateTo: NaiveDate!) {
		eligibleRevenuesForInvoice(customerId: $customerId, dateFrom: $dateFrom, dateTo: $dateTo) {
			revenueId
			accountId
			accountName
			amount
			revenue {
				id
				amount
				startDate
				endDate
				waveServiceId
			}
		}
	}
`;

// Query for eligible projects to add to an invoice
export const ELIGIBLE_PROJECTS_QUERY = gql`
	query EligibleProjectsForInvoice($customerId: UUID!, $dateFrom: NaiveDate!, $dateTo: NaiveDate!) {
		eligibleProjectsForInvoice(customerId: $customerId, dateFrom: $dateFrom, dateTo: $dateTo) {
			projectId
			name
			date
			amount
			project {
				id
				name
				date
				amount
				formattedAddress
				waveServiceId
			}
		}
	}
`;

// Types
export type InvoiceStatus = 'DRAFT' | 'SENT' | 'PAID' | 'OVERDUE' | 'CANCELLED';

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

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

export interface InvoiceRevenueEntry {
	id: string;
	revenueId: string;
	amount: number;
	account: InvoiceAccount | null;
	revenue: {
		id: string;
		amount: number;
		startDate: string;
		endDate: string | null;
		waveServiceId: string | null;
		isActive: boolean;
	} | null;
}

export interface InvoiceProjectEntry {
	id: string;
	projectId: string;
	amount: number;
	account: InvoiceAccount | null;
	project: {
		id: string;
		name: string;
		date: string;
		amount: number | null;
		formattedAddress: string | null;
		waveServiceId: string | null;
	} | null;
}

export interface InvoiceListItem {
	id: string;
	customerId: string;
	startDate: string;
	endDate: string;
	status: InvoiceStatus;
	datePaid: string | null;
	waveInvoiceId: string | null;
	customer: InvoiceCustomer | null;
	revenuesTotal: number;
	projectsTotal: number;
	totalAmount: number;
	revenueCount: number;
	projectCount: number;
}

export interface InvoiceDetail {
	id: string;
	customerId: string;
	startDate: string;
	endDate: string;
	status: InvoiceStatus;
	datePaid: string | null;
	waveInvoiceId: string | null;
	customer: InvoiceCustomer | null;
	revenues: InvoiceRevenueEntry[];
	projects: InvoiceProjectEntry[];
	revenuesTotal: number;
	projectsTotal: number;
	totalAmount: number;
	revenueCount: number;
	projectCount: number;
}

export interface InvoicesQueryResult {
	invoices: {
		items: InvoiceListItem[];
		totalCount: number;
		hasNextPage: boolean;
	};
}

export interface InvoiceQueryResult {
	invoice: InvoiceDetail | null;
}

export interface EligibleRevenue {
	revenueId: string;
	accountId: string;
	accountName: string;
	amount: number;
	revenue: {
		id: string;
		amount: number;
		startDate: string;
		endDate: string | null;
		waveServiceId: string | null;
	} | null;
}

export interface EligibleInvoiceProject {
	projectId: string;
	name: string;
	date: string;
	amount: number;
	project: {
		id: string;
		name: string;
		date: string;
		amount: number | null;
		formattedAddress: string | null;
		waveServiceId: string | null;
	} | null;
}

export interface EligibleRevenuesQueryResult {
	eligibleRevenuesForInvoice: EligibleRevenue[];
}

export interface EligibleProjectsQueryResult {
	eligibleProjectsForInvoice: EligibleInvoiceProject[];
}

export interface InvoiceFilterInput {
	customerId?: string;
	status?: InvoiceStatus;
	date?: string;
	startDateFrom?: string;
	endDateTo?: string;
}
