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

// Types
export interface WaveMoney {
	value: string;
	currencyCode: string;
	currencySymbol: string;
}

export interface WaveProduct {
	id: string;
	name: string;
	description?: string;
	unitPrice: number;
	isSold: boolean;
	isArchived: boolean;
	incomeAccount?: {
		id: string;
		name: string;
	};
}

export interface WaveCustomer {
	id: string;
	name: string;
	email?: string;
	currencyCode?: string;
	address?: {
		addressLine1?: string;
		addressLine2?: string;
		city?: string;
		provinceCode?: string;
		provinceName?: string;
		postalCode?: string;
	};
}

export interface WaveInvoiceItem {
	description: string;
	quantity: number;
	unitPrice: number;
	total: WaveMoney;
}

export interface WaveInvoice {
	id: string;
	invoiceNumber: string;
	invoiceDate: string;
	dueDate?: string;
	status: string;
	customer: {
		id: string;
		name: string;
	};
	items: WaveInvoiceItem[];
	subtotal: WaveMoney;
	total: WaveMoney;
	amountDue: WaveMoney;
	amountPaid: WaveMoney;
	pdfUrl?: string;
}

export interface WaveInvoiceListItem {
	id: string;
	invoiceNumber: string;
	invoiceDate: string;
	dueDate?: string;
	status: string;
	customer: {
		id: string;
		name: string;
	};
	subtotal: WaveMoney;
	total: WaveMoney;
	amountDue: WaveMoney;
	amountPaid: WaveMoney;
}

export interface WavePageInfo {
	currentPage: number;
	totalPages: number;
	totalCount: number;
}

export interface WaveInvoiceConnection {
	pageInfo: WavePageInfo;
	invoices: WaveInvoiceListItem[];
}

export interface WaveInvoiceReadiness {
	ready: boolean;
	issues: string[];
	waveCustomerId?: string;
	readyItemCount: number;
	missingWaveLinkCount: number;
	totalAmount: string;
}

// Queries
export const WAVE_PRODUCTS = gql`
	query WaveProducts {
		waveProducts {
			id
			name
			description
			unitPrice
			isSold
			isArchived
		}
	}
`;

export const WAVE_CUSTOMERS = gql`
	query WaveCustomers {
		waveCustomers {
			id
			name
			email
			currencyCode
		}
	}
`;

export const WAVE_INVOICE = gql`
	query WaveInvoice($invoiceId: String!) {
		waveInvoice(invoiceId: $invoiceId) {
			id
			invoiceNumber
			invoiceDate
			dueDate
			status
			customer {
				id
				name
			}
			items {
				description
				quantity
				unitPrice
				total {
					value
					currencyCode
					currencySymbol
				}
			}
			subtotal {
				value
				currencyCode
				currencySymbol
			}
			total {
				value
				currencyCode
				currencySymbol
			}
			amountDue {
				value
				currencyCode
				currencySymbol
			}
			amountPaid {
				value
				currencyCode
				currencySymbol
			}
			pdfUrl
		}
	}
`;

export const WAVE_INVOICE_READINESS = gql`
	query WaveInvoiceReadiness($invoiceId: UUID!) {
		waveInvoiceReadiness(invoiceId: $invoiceId) {
			ready
			issues
			waveCustomerId
			readyItemCount
			missingWaveLinkCount
			totalAmount
		}
	}
`;

export const WAVE_INVOICES = gql`
	query WaveInvoices($page: Int, $pageSize: Int) {
		waveInvoices(page: $page, pageSize: $pageSize) {
			pageInfo {
				currentPage
				totalPages
				totalCount
			}
			invoices {
				id
				invoiceNumber
				invoiceDate
				dueDate
				status
				customer {
					id
					name
				}
				subtotal {
					value
					currencyCode
					currencySymbol
				}
				total {
					value
					currencyCode
					currencySymbol
				}
				amountDue {
					value
					currencyCode
					currencySymbol
				}
				amountPaid {
					value
					currencyCode
					currencySymbol
				}
			}
		}
	}
`;

export const WAVE_CUSTOMER = gql`
	query WaveCustomer($customerId: String!) {
		waveCustomer(customerId: $customerId) {
			id
			name
			email
			currencyCode
			address {
				addressLine1
				addressLine2
				city
				provinceCode
				provinceName
				postalCode
			}
		}
	}
`;

export const WAVE_PRODUCT = gql`
	query WaveProduct($productId: String!) {
		waveProduct(productId: $productId) {
			id
			name
			description
			unitPrice
			isSold
			isArchived
			incomeAccount {
				id
				name
			}
		}
	}
`;
