CREATE TABLE `capex_lignes` (
	`id` int AUTO_INCREMENT NOT NULL,
	`annee` int NOT NULL,
	`etablissementCode` varchar(50) NOT NULL,
	`cle` varchar(100) NOT NULL,
	`montant` decimal(12,2),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `capex_lignes_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `etablissements` (
	`id` int AUTO_INCREMENT NOT NULL,
	`code` varchar(50) NOT NULL,
	`nom` varchar(255) NOT NULL,
	`groupe` varchar(100),
	`ville` varchar(100),
	`actif` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `etablissements_id` PRIMARY KEY(`id`),
	CONSTRAINT `etablissements_code_unique` UNIQUE(`code`)
);
--> statement-breakpoint
CREATE TABLE `inventaire_meta` (
	`id` int AUTO_INCREMENT NOT NULL,
	`annee` int NOT NULL,
	`filename` varchar(255),
	`dateImport` timestamp NOT NULL DEFAULT (now()),
	`nbEtablissements` int DEFAULT 0,
	`nbFixes` int DEFAULT 0,
	`nbPortables` int DEFAULT 0,
	CONSTRAINT `inventaire_meta_id` PRIMARY KEY(`id`),
	CONSTRAINT `inventaire_meta_annee_unique` UNIQUE(`annee`)
);
--> statement-breakpoint
CREATE TABLE `inventaire_postes` (
	`id` int AUTO_INCREMENT NOT NULL,
	`annee` int NOT NULL,
	`etablissementCode` varchar(50) NOT NULL,
	`libelle` varchar(255),
	`typePoste` enum('fixe','portable') NOT NULL,
	`dateRef` varchar(20),
	`ageAns` decimal(5,2),
	`modele` varchar(255),
	`fabricant` varchar(100),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `inventaire_postes_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `opex_montants_etab` (
	`id` int AUTO_INCREMENT NOT NULL,
	`annee` int NOT NULL,
	`etablissementCode` varchar(50) NOT NULL,
	`libellePoste` varchar(255) NOT NULL,
	`montant` decimal(12,2),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `opex_montants_etab_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `opex_postes` (
	`id` int AUTO_INCREMENT NOT NULL,
	`annee` int NOT NULL,
	`colIdx` int NOT NULL,
	`libelle` varchar(255) NOT NULL,
	`libelleCourt` varchar(100),
	`libelleDetail` varchar(255),
	`fournisseur` varchar(255),
	`categorie` varchar(100),
	`type` varchar(100),
	`facturation` varchar(100),
	`modeVentilation` varchar(50) DEFAULT 'Prorata C 6',
	`compte` varchar(50),
	`detail` text,
	`budgetN1` decimal(12,2),
	`montant` decimal(12,2),
	`isCustom` boolean DEFAULT false,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `opex_postes_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `opex_validated` (
	`id` int AUTO_INCREMENT NOT NULL,
	`annee` int NOT NULL,
	`validatedAt` timestamp NOT NULL DEFAULT (now()),
	`validatedBy` int,
	CONSTRAINT `opex_validated_id` PRIMARY KEY(`id`),
	CONSTRAINT `opex_validated_annee_unique` UNIQUE(`annee`)
);
--> statement-breakpoint
CREATE TABLE `parametres_app` (
	`id` int AUTO_INCREMENT NOT NULL,
	`cle` varchar(100) NOT NULL,
	`valeur` text,
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `parametres_app_id` PRIMARY KEY(`id`),
	CONSTRAINT `parametres_app_cle_unique` UNIQUE(`cle`)
);
--> statement-breakpoint
CREATE TABLE `user_etablissements` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`etablissementCode` varchar(50) NOT NULL,
	CONSTRAINT `user_etablissements_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `users` (
	`id` int AUTO_INCREMENT NOT NULL,
	`login` varchar(255) NOT NULL,
	`email` varchar(320),
	`passwordHash` varchar(255) NOT NULL,
	`firstName` varchar(100),
	`lastName` varchar(100),
	`role` enum('admin','standard','readonly') NOT NULL DEFAULT 'standard',
	`isActive` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	`lastSignedIn` timestamp,
	CONSTRAINT `users_id` PRIMARY KEY(`id`),
	CONSTRAINT `users_login_unique` UNIQUE(`login`)
);
