CREATE TABLE `importLogs` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`sourceFileId` int NOT NULL,
	`fileName` varchar(255) NOT NULL,
	`totalInvoicesDetected` int NOT NULL DEFAULT 0,
	`invoicesImported` int NOT NULL DEFAULT 0,
	`duplicatesIgnored` int NOT NULL DEFAULT 0,
	`errors` int NOT NULL DEFAULT 0,
	`duplicateDetails` text,
	`errorDetails` text,
	`importedAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `importLogs_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `invoices` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`sourceFileId` int NOT NULL,
	`invoiceIndexInFile` int NOT NULL DEFAULT 1,
	`fileName` varchar(255) NOT NULL,
	`fileKey` text NOT NULL,
	`fileUrl` text NOT NULL,
	`supplierName` varchar(255),
	`invoiceNumber` varchar(100),
	`invoiceDate` timestamp,
	`deliveryNoteNumber` varchar(100),
	`orderNumber` varchar(100),
	`totalAmount` decimal(10,2),
	`pageRange` varchar(20),
	`qualityScore` int,
	`metadataFileKey` text,
	`metadataFileUrl` text,
	`status` enum('processing','completed','error') NOT NULL DEFAULT 'processing',
	`errorMessage` text,
	`manuallyEdited` int NOT NULL DEFAULT 0,
	`exportedAt` timestamp,
	`exportMode` enum('manual','automatic'),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `invoices_id` PRIMARY KEY(`id`),
	CONSTRAINT `supplier_invoice_date_unique` UNIQUE(`supplierName`,`invoiceNumber`,`invoiceDate`)
);
--> statement-breakpoint
CREATE TABLE `llmLogs` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`sourceFileId` int,
	`invoiceId` int,
	`operation` varchar(50) NOT NULL,
	`model` varchar(50) NOT NULL,
	`promptSent` text NOT NULL,
	`rawResponse` text NOT NULL,
	`cleanedResponse` text,
	`success` int NOT NULL DEFAULT 1,
	`errorMessage` text,
	`processingTimeMs` int,
	`pageRange` varchar(50),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `llmLogs_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `sourceFiles` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`fileName` varchar(255) NOT NULL,
	`fileKey` text NOT NULL,
	`fileUrl` text NOT NULL,
	`totalInvoicesDetected` int NOT NULL DEFAULT 0,
	`processingStatus` enum('processing','completed','error') NOT NULL DEFAULT 'processing',
	`processingProgress` varchar(255),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `sourceFiles_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `userSettings` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`llmModel` varchar(50) NOT NULL DEFAULT 'mistral-large-latest',
	`orderNumberFormat` text,
	`invoiceNumberKeywords` text,
	`deliveryNoteKeywords` text,
	`orderNumberKeywords` text,
	`supplierKeywords` text,
	`totalAmountKeywords` text,
	`sftpHost` varchar(255),
	`sftpPort` int DEFAULT 22,
	`sftpUsername` varchar(255),
	`sftpPassword` text,
	`sftpRemotePath` varchar(500) DEFAULT '/',
	`sftpAutoExport` int NOT NULL DEFAULT 0,
	`llmLogsRetentionMonths` int NOT NULL DEFAULT 3,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `userSettings_id` PRIMARY KEY(`id`),
	CONSTRAINT `userSettings_userId_unique` UNIQUE(`userId`)
);
--> statement-breakpoint
ALTER TABLE `users` MODIFY COLUMN `openId` varchar(64);--> statement-breakpoint
ALTER TABLE `users` MODIFY COLUMN `email` varchar(320) NOT NULL;--> statement-breakpoint
ALTER TABLE `users` MODIFY COLUMN `loginMethod` enum('manus','local','azure-ad') NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `azureAdId` varchar(64);--> statement-breakpoint
ALTER TABLE `users` ADD `passwordHash` varchar(255);--> statement-breakpoint
ALTER TABLE `users` ADD `isActive` int DEFAULT 1 NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD CONSTRAINT `users_azureAdId_unique` UNIQUE(`azureAdId`);--> statement-breakpoint
ALTER TABLE `users` ADD CONSTRAINT `users_email_unique` UNIQUE(`email`);