BEGIN TRY

BEGIN TRAN;

-- CreateTable
CREATE TABLE [dbo].[materiel] (
    [id] INT NOT NULL IDENTITY(1,1),
    [nom] NVARCHAR(1000) NOT NULL,
    [marque] NVARCHAR(1000) NOT NULL,
    [modele] NVARCHAR(1000) NOT NULL,
    [reference] NVARCHAR(1000) NOT NULL,
    [numero_inventaire] NVARCHAR(1000) NOT NULL,
    [numero_serie] NVARCHAR(1000) NOT NULL,
    [statut] NVARCHAR(1000) NOT NULL,
    [etat_general] NVARCHAR(1000) NOT NULL,
    [categorie_id] INT NOT NULL,
    [campus_id] INT NOT NULL,
    [actif] BIT NOT NULL CONSTRAINT [materiel_actif_df] DEFAULT 1,
    [created_at] DATETIME2 NOT NULL CONSTRAINT [materiel_created_at_df] DEFAULT CURRENT_TIMESTAMP,
    [updated_at] DATETIME2 NOT NULL,
    CONSTRAINT [materiel_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- CreateTable
CREATE TABLE [dbo].[accessoire] (
    [id] INT NOT NULL IDENTITY(1,1),
    [nom] NVARCHAR(1000) NOT NULL,
    [description] NVARCHAR(1000) NOT NULL,
    [actif] BIT NOT NULL CONSTRAINT [accessoire_actif_df] DEFAULT 1,
    [created_at] DATETIME2 NOT NULL CONSTRAINT [accessoire_created_at_df] DEFAULT CURRENT_TIMESTAMP,
    [updated_at] DATETIME2 NOT NULL,
    CONSTRAINT [accessoire_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- CreateTable
CREATE TABLE [dbo].[materiel_accessoire] (
    [id] INT NOT NULL IDENTITY(1,1),
    [materiel_id] INT NOT NULL,
    [accessoire_id] INT NOT NULL,
    [quantite_attendue] INT NOT NULL,
    [obligatoire] BIT NOT NULL,
    CONSTRAINT [materiel_accessoire_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- AddForeignKey
ALTER TABLE [dbo].[materiel] ADD CONSTRAINT [materiel_categorie_id_fkey] FOREIGN KEY ([categorie_id]) REFERENCES [dbo].[categorie_materiel]([id]) ON DELETE NO ACTION ON UPDATE CASCADE;

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

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

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

COMMIT TRAN;

END TRY
BEGIN CATCH

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

END CATCH
