BEGIN TRY

BEGIN TRAN;

-- CreateTable
CREATE TABLE [dbo].[emprunt] (
    [id] INT NOT NULL IDENTITY(1,1),
    [utilisateur_id] INT NOT NULL,
    [materiel_id] INT NOT NULL,
    [campus_id] INT NOT NULL,
    [salle_pret_id] INT NOT NULL,
    [poste_emprunt_id] INT NOT NULL,
    [date_emprunt] DATETIME2 NOT NULL,
    [date_retour_prevue] DATETIME2 NOT NULL,
    [date_retour_reelle] DATETIME2,
    [statut] NVARCHAR(1000) NOT NULL,
    [mode_identification_emprunt] NVARCHAR(1000) NOT NULL,
    [mode_identification_retour] NVARCHAR(1000),
    [commentaire_depart] NVARCHAR(1000),
    [commentaire_retour] NVARCHAR(1000),
    [created_at] DATETIME2 NOT NULL CONSTRAINT [emprunt_created_at_df] DEFAULT CURRENT_TIMESTAMP,
    [updated_at] DATETIME2 NOT NULL,
    CONSTRAINT [emprunt_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- CreateTable
CREATE TABLE [dbo].[checklist] (
    [id] INT NOT NULL IDENTITY(1,1),
    [emprunt_id] INT NOT NULL,
    [utilisateur_id] INT NOT NULL,
    [type] NVARCHAR(1000) NOT NULL,
    [date_verification] DATETIME2 NOT NULL,
    [commentaire] NVARCHAR(1000),
    [created_at] DATETIME2 NOT NULL CONSTRAINT [checklist_created_at_df] DEFAULT CURRENT_TIMESTAMP,
    [updated_at] DATETIME2 NOT NULL,
    CONSTRAINT [checklist_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- CreateTable
CREATE TABLE [dbo].[checklist_element] (
    [id] INT NOT NULL IDENTITY(1,1),
    [checklist_id] INT NOT NULL,
    [accessoire_id] INT,
    [nom_element] NVARCHAR(1000) NOT NULL,
    [etat] NVARCHAR(1000) NOT NULL,
    [quantite_constatee] INT NOT NULL,
    [commentaire] NVARCHAR(1000),
    [created_at] DATETIME2 NOT NULL CONSTRAINT [checklist_element_created_at_df] DEFAULT CURRENT_TIMESTAMP,
    [updated_at] DATETIME2 NOT NULL,
    CONSTRAINT [checklist_element_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- AddForeignKey
ALTER TABLE [dbo].[emprunt] ADD CONSTRAINT [emprunt_utilisateur_id_fkey] FOREIGN KEY ([utilisateur_id]) REFERENCES [dbo].[utilisateur]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[emprunt] ADD CONSTRAINT [emprunt_materiel_id_fkey] FOREIGN KEY ([materiel_id]) REFERENCES [dbo].[materiel]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[emprunt] ADD CONSTRAINT [emprunt_campus_id_fkey] FOREIGN KEY ([campus_id]) REFERENCES [dbo].[campus]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[emprunt] ADD CONSTRAINT [emprunt_salle_pret_id_fkey] FOREIGN KEY ([salle_pret_id]) REFERENCES [dbo].[salle_pret]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[emprunt] ADD CONSTRAINT [emprunt_poste_emprunt_id_fkey] FOREIGN KEY ([poste_emprunt_id]) REFERENCES [dbo].[poste_emprunt]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[checklist] ADD CONSTRAINT [checklist_emprunt_id_fkey] FOREIGN KEY ([emprunt_id]) REFERENCES [dbo].[emprunt]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[checklist] ADD CONSTRAINT [checklist_utilisateur_id_fkey] FOREIGN KEY ([utilisateur_id]) REFERENCES [dbo].[utilisateur]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[checklist_element] ADD CONSTRAINT [checklist_element_checklist_id_fkey] FOREIGN KEY ([checklist_id]) REFERENCES [dbo].[checklist]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE [dbo].[checklist_element] ADD CONSTRAINT [checklist_element_accessoire_id_fkey] FOREIGN KEY ([accessoire_id]) REFERENCES [dbo].[accessoire]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

COMMIT TRAN;

END TRY
BEGIN CATCH

IF @@TRANCOUNT > 0
BEGIN
    ROLLBACK TRAN;
END;
THROW

END CATCH
