import { writable, derived } from 'svelte/store';


// This file is holy and shall not be edited by Copilot!


// User session and authentication
export const session = writable<{
  token?: string;
  user?: { name: string; email: string };
}>({});

// Data structure column mapping
export interface ColumnMappingType {
	name: number;
	nationality: number;
	birthday: number;
	pictureUrl: number;
	alreadyPrinted: number;
	sheetName: string;
}

// Data structure for a row in the sheet
export interface RowData {
	id: string; // Unique identifier
	name: string;
	nationality: string;
	birthday: string;
	pictureUrl: string;
	alreadyPrinted: boolean;
	_rowIndex: number;
	_checked: boolean;
	_valid: boolean;
}

// Picture storage and metadata
export interface PictureBlobInfoType {
	id: string;
	url: string;
	downloaded: boolean;
	faceDetected: boolean;
	faceCount: number;
}

// CropType rectangles for each photo
export interface CropType {
	x: number;
	y: number;
	width: number;
	height: number;
}

// Google Sheets list for search
export interface SheetInfoType {
	id: string;
	name: string;
	webViewLink: string;
}

// Card details type
export interface CardDetailsType {
	esnSection: string;
	studiesAt: string;
	validityStart: string;
}



// Column mapping configuration
export const columnMapping = writable<ColumnMappingType>({
	name: -1,
	nationality: -1,
	birthday: -1,
	pictureUrl: -1,
	alreadyPrinted: -1,
	sheetName: ''
});

// Store to hold the processed sheet data
export const sheetData = writable<RowData[]>([]);

// Store and hold the processed picture data
export const pictures = writable<Record<string, PictureBlobInfoType>>({});

// Store and hold the crop rectangles from face detection
export const cropRects = writable<Record<string, CropType>>({});

// Store and hold the selected sheet
export const selectedSheet = writable<SheetInfoType>({ id: '', name: '', webViewLink: '' });

// Card details for generation
export const cardDetails = writable<CardDetailsType | null>(null);

// Selected card type for generation
import type { Card } from '$lib/cards/types';
export const selectedCard = writable<Card | null>(null);

// Wizard state management
export const currentStep = writable<number>(0);

export const steps = [
	'splash',
	'auth',
	'search',
	'mapping',
	'validation',
	'card-details',
	'card-select',
	'gallery',
	'generate'
] as const;

export type WizardStep = typeof steps[number];

export const currentStepName = derived(
	currentStep,
	($currentStep) => steps[$currentStep]
);

// Progress tracking
export interface ProgressState {
	stage: string;
	current: number;
	total: number;
	message: string;
}

export const progress = writable<ProgressState>({
	stage: '',
	current: 0,
	total: 0,
	message: ''
});
