CREATE TABLE `associationDirectoryEntries` (
	`id` int AUTO_INCREMENT NOT NULL,
	`nomAssociation` varchar(255) NOT NULL,
	`emailOfficiel` varchar(320),
	`emailOfficielNormalise` varchar(320),
	`siret` varchar(14),
	`rna` varchar(10),
	`thematique` text,
	`adresse` text,
	`codePostal` varchar(10),
	`ville` varchar(100),
	`telephone` varchar(20),
	`siteWeb` varchar(255),
	`facebookUrl` varchar(255),
	`instagramUrl` varchar(255),
	`dateCreation` timestamp,
	`objetAssociation` text,
	`statutJuridique` enum('association_loi_1901','association_reconnue_utilite_publique','fondation','autre') DEFAULT 'association_loi_1901',
	`nomRepresentant` varchar(255),
	`fonctionRepresentant` varchar(100),
	`gouvernance` text,
	`latitude` varchar(32),
	`longitude` varchar(32),
	`geoSource` enum('manual','adresse_gouv','dataasso','commune_center'),
	`geoPrecision` enum('exact_address','commune_center','hidden') NOT NULL DEFAULT 'commune_center',
	`geoLastSyncedAt` timestamp,
	`externalSourceStatus` varchar(100),
	`externalSourceLabel` varchar(255),
	`associationStatus` varchar(40),
	`registryLastUpdatedAt` timestamp,
	`referenceLastCheckedAt` timestamp,
	`referenceStatus` varchar(100),
	`referenceSourceLabel` varchar(255),
	`sourceFileName` varchar(255),
	`sourceRowNumber` int,
	`sourceFingerprint` varchar(64),
	`isActive` boolean NOT NULL DEFAULT true,
	`importedAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `associationDirectoryEntries_id` PRIMARY KEY(`id`),
	CONSTRAINT `associationDirectoryEntries_emailOfficielNormalise_unique` UNIQUE(`emailOfficielNormalise`)
);
--> statement-breakpoint
CREATE TABLE `associationDirectoryReviews` (
	`id` int AUTO_INCREMENT NOT NULL,
	`sourceType` enum('portal_signup','helloasso') NOT NULL,
	`sourceLabel` varchar(255),
	`userId` int,
	`resolvedDirectoryEntryId` int,
	`proposedNomAssociation` varchar(255) NOT NULL,
	`proposedEmail` varchar(320),
	`proposedEmailNormalise` varchar(320),
	`proposedSiret` varchar(14),
	`proposedRna` varchar(10),
	`proposedVille` varchar(100),
	`matchStatus` varchar(40),
	`payload` text,
	`status` enum('pending','linked','created','ignored') NOT NULL DEFAULT 'pending',
	`resolutionNote` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `associationDirectoryReviews_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `associationInvitations` (
	`id` int AUTO_INCREMENT NOT NULL,
	`directoryEntryId` int NOT NULL,
	`emailOfficiel` varchar(320) NOT NULL,
	`emailOfficielNormalise` varchar(320) NOT NULL,
	`token` varchar(128) NOT NULL,
	`deliveryMode` enum('email','manual_link') NOT NULL,
	`emailSent` boolean NOT NULL DEFAULT false,
	`sentByUserId` int NOT NULL,
	`sentAt` timestamp NOT NULL DEFAULT (now()),
	`expiresAt` timestamp NOT NULL,
	`usedAt` timestamp,
	`revokedAt` timestamp,
	`acceptedByUserId` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `associationInvitations_id` PRIMARY KEY(`id`),
	CONSTRAINT `associationInvitations_token_unique` UNIQUE(`token`)
);
--> statement-breakpoint
CREATE TABLE `materialReturnFollowups` (
	`id` int AUTO_INCREMENT NOT NULL,
	`requestId` int NOT NULL,
	`associationId` int NOT NULL,
	`serviceLabel` varchar(255),
	`recipientEmails` text,
	`supervisionServiceLabel` varchar(255),
	`supervisionRecipientEmails` text,
	`restitutionDate` timestamp NOT NULL,
	`plannedSendAt` timestamp NOT NULL,
	`sentAt` timestamp,
	`status` enum('planifie','en_attente','en_cours','cloture') NOT NULL DEFAULT 'planifie',
	`uploadToken` varchar(96) NOT NULL,
	`uploadTokenExpiresAt` timestamp NOT NULL,
	`signedFileKey` varchar(512),
	`signedFileUrl` varchar(1024),
	`signedFileName` varchar(255),
	`signedMimeType` varchar(100),
	`uploadedByName` varchar(255),
	`uploadedByEmail` varchar(320),
	`agentUserId` int,
	`agentRole` varchar(100),
	`borrowerName` varchar(255),
	`borrowerRole` varchar(120),
	`compliance` enum('conforme','non_conforme'),
	`discrepancyCategories` text,
	`discrepancyDetails` text,
	`issueFlag` boolean NOT NULL DEFAULT false,
	`litigationStatus` enum('none','pending','resolved') NOT NULL DEFAULT 'none',
	`blockedItems` text,
	`estimatedDamageAmount` int,
	`estimatedDamageSource` varchar(32),
	`arbitrationDecision` enum('partial_retention','full_retention','dismissed'),
	`arbitrationAmount` int,
	`arbitrationNotes` text,
	`arbitratedByUserId` int,
	`arbitratedAt` timestamp,
	`litigationLetterKey` varchar(512),
	`litigationLetterUrl` varchar(1024),
	`litigationLetterName` varchar(255),
	`agentSignatureKey` varchar(512),
	`agentSignatureUrl` varchar(1024),
	`borrowerSignatureKey` varchar(512),
	`borrowerSignatureUrl` varchar(1024),
	`finalPdfKey` varchar(512),
	`finalPdfUrl` varchar(1024),
	`finalPdfName` varchar(255),
	`geoLatitude` varchar(64),
	`geoLongitude` varchar(64),
	`geoStatus` varchar(32),
	`geoFailureReason` text,
	`uploadedAt` timestamp,
	`validatedAt` timestamp,
	`closedAt` timestamp,
	`lastReminderSentAt` timestamp,
	`reactivatedByUserId` int,
	`reactivatedAt` timestamp,
	`reactivationReason` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `materialReturnFollowups_id` PRIMARY KEY(`id`),
	CONSTRAINT `materialReturnFollowups_requestId_unique` UNIQUE(`requestId`),
	CONSTRAINT `materialReturnFollowups_uploadToken_unique` UNIQUE(`uploadToken`)
);
--> statement-breakpoint
CREATE TABLE `operationalRecapServices` (
	`id` int AUTO_INCREMENT NOT NULL,
	`label` varchar(255) NOT NULL,
	`usage` enum('terrain','controle') NOT NULL DEFAULT 'terrain',
	`description` text,
	`recipientEmails` text NOT NULL,
	`actif` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `operationalRecapServices_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `emailActionTokens` MODIFY COLUMN `action` enum('validee','refusee','acceptation_devis_salle','refus_devis_salle') NOT NULL;--> statement-breakpoint
ALTER TABLE `requestTemplates` MODIFY COLUMN `type` enum('subvention_fonctionnement','subvention_projet','agrement_jeunesse_education','agrement_sport','autorisation_occupation','demande_salle','demande_materiel_evenementiel','autre') NOT NULL;--> statement-breakpoint
ALTER TABLE `requests` MODIFY COLUMN `type` enum('subvention_fonctionnement','subvention_projet','agrement_jeunesse_education','agrement_sport','autorisation_occupation','demande_salle','demande_materiel_evenementiel','autre') NOT NULL;--> statement-breakpoint
ALTER TABLE `users` MODIFY COLUMN `role` enum('user','accueil','service_terrain','logistique_controle','admin','directrice','super_admin') NOT NULL DEFAULT 'user';--> statement-breakpoint
ALTER TABLE `associations` ADD `sourceDirectoryEntryId` int;--> statement-breakpoint
ALTER TABLE `associations` ADD `thematique` text;--> statement-breakpoint
ALTER TABLE `associations` ADD `facebookUrl` varchar(255);--> statement-breakpoint
ALTER TABLE `associations` ADD `instagramUrl` varchar(255);--> statement-breakpoint
ALTER TABLE `associations` ADD `gouvernance` text;--> statement-breakpoint
ALTER TABLE `users` ADD `passwordHash` varchar(255);--> statement-breakpoint
ALTER TABLE `users` ADD `canManageLogistics` boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `delegatedSalleSignerUserId` int;--> statement-breakpoint
ALTER TABLE `users` ADD `failedLoginAttempts` int DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `lockedUntil` timestamp;--> statement-breakpoint
ALTER TABLE `users` ADD `mfaEnabled` boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `mfaMethod` varchar(32);--> statement-breakpoint
ALTER TABLE `users` ADD `mfaChallengeToken` varchar(96);--> statement-breakpoint
ALTER TABLE `users` ADD `mfaCodeHash` varchar(255);--> statement-breakpoint
ALTER TABLE `users` ADD `mfaCodeExpiresAt` timestamp;--> statement-breakpoint
ALTER TABLE `users` ADD `mfaCodeAttempts` int DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `mfaTotpSecretEncrypted` varchar(512);--> statement-breakpoint
ALTER TABLE `users` ADD `mfaTotpPendingSecretEncrypted` varchar(512);--> statement-breakpoint
ALTER TABLE `users` ADD `deletionRequestedAt` timestamp;--> statement-breakpoint
ALTER TABLE `users` ADD `purgeScheduledAt` timestamp;--> statement-breakpoint
ALTER TABLE `users` ADD `purgedAt` timestamp;--> statement-breakpoint
ALTER TABLE `users` ADD `legalHold` boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `users` ADD `legalHoldReason` text;--> statement-breakpoint
ALTER TABLE `users` ADD `privacyConsentVersion` varchar(32);--> statement-breakpoint
ALTER TABLE `users` ADD `privacyConsentAcceptedAt` timestamp;--> statement-breakpoint
ALTER TABLE `users` ADD `privacyConsentContext` varchar(64);