/*
    Script de création des tables SQL Server pour la synchronisation Meta Lead Gen.

    IMPORTANT :
    Avant de lancer ce script, la base de données doit déjà exister.

    Étapes :
    1. Se connecter à SQL Server avec un outil comme Adminer, SSMS, Azure Data Studio ou DBeaver.
    2. Créer la base de données si elle n'existe pas encore :

       CREATE DATABASE MetaLeadDb;

    3. Se connecter ensuite à la base MetaLeadDb.
    4. Lancer ce script pour créer les tables :
       - leadgen_forms
       - lead_data
       - sync_logs

    Ce script ne crée pas la base de données.
    Il crée uniquement les tables nécessaires au projet.*/


    IF OBJECT_ID(N'dbo.sync_logs', N'U') IS NOT NULL
    DROP TABLE dbo.sync_logs;

IF OBJECT_ID(N'dbo.lead_data', N'U') IS NOT NULL
    DROP TABLE dbo.lead_data;

IF OBJECT_ID(N'dbo.leadgen_forms', N'U') IS NOT NULL
    DROP TABLE dbo.leadgen_forms;

CREATE TABLE dbo.leadgen_forms (
    id BIGINT IDENTITY(1,1) NOT NULL,
    meta_form_id NVARCHAR(64) NOT NULL,
    page_id NVARCHAR(64) NOT NULL,
    name NVARCHAR(255) NOT NULL,
    locale NVARCHAR(20) NULL,
    status NVARCHAR(50) NOT NULL,
    created_at DATETIME2 NOT NULL CONSTRAINT DF_leadgen_forms_created_at DEFAULT SYSDATETIME(),
    updated_at DATETIME2 NULL,

    CONSTRAINT PK_leadgen_forms PRIMARY KEY (id),
    CONSTRAINT UQ_leadgen_forms_meta_form_id UNIQUE (meta_form_id)
);

CREATE TABLE dbo.lead_data (
    id BIGINT IDENTITY(1,1) NOT NULL,
    meta_lead_id NVARCHAR(64) NOT NULL,
    meta_form_id NVARCHAR(64) NOT NULL,
    created_time DATETIME2 NULL,
    je_suis NVARCHAR(255) NULL,
    nom NVARCHAR(255) NULL,
    first_name NVARCHAR(255) NULL,
    adresse_email NVARCHAR(255) NULL,
    phone_number NVARCHAR(50) NULL,
    city NVARCHAR(255) NULL,
    campus NVARCHAR(100) NULL,
    status NVARCHAR(50) NOT NULL CONSTRAINT DF_lead_data_status DEFAULT N'RECEIVED',
    error_message NVARCHAR(MAX) NULL,
    synced_at DATETIME2 NULL,
    created_at DATETIME2 NOT NULL CONSTRAINT DF_lead_data_created_at DEFAULT SYSDATETIME(),
    updated_at DATETIME2 NULL,

    CONSTRAINT PK_lead_data PRIMARY KEY (id),
    CONSTRAINT UQ_lead_data_meta_lead_id UNIQUE (meta_lead_id),

    CONSTRAINT FK_lead_data_leadgen_forms
        FOREIGN KEY (meta_form_id)
        REFERENCES dbo.leadgen_forms(meta_form_id)
        ON UPDATE CASCADE
        ON DELETE NO ACTION,

    CONSTRAINT CK_lead_data_status
        CHECK (status IN (N'RECEIVED', N'READY_TO_SEND', N'SENT', N'ERROR', N'INVALID'))
);

CREATE TABLE dbo.sync_logs (
    id BIGINT IDENTITY(1,1) NOT NULL,
    lead_id BIGINT NOT NULL,
    oscar_payload NVARCHAR(MAX) NULL,
    oscar_response NVARCHAR(MAX) NULL,
    status NVARCHAR(50) NOT NULL,
    error_message NVARCHAR(MAX) NULL,
    attempted_at DATETIME2 NOT NULL CONSTRAINT DF_sync_logs_attempted_at DEFAULT SYSDATETIME(),

    CONSTRAINT PK_sync_logs PRIMARY KEY (id),

    CONSTRAINT FK_sync_logs_lead_data
        FOREIGN KEY (lead_id)
        REFERENCES dbo.lead_data(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,

    CONSTRAINT CK_sync_logs_status
        CHECK (status IN (N'SENT', N'ERROR', N'INVALID'))
);

CREATE INDEX IX_leadgen_forms_status
ON dbo.leadgen_forms(status);

CREATE INDEX IX_lead_data_meta_form_id
ON dbo.lead_data(meta_form_id);

CREATE INDEX IX_lead_data_status
ON dbo.lead_data(status);

CREATE INDEX IX_lead_data_created_time
ON dbo.lead_data(created_time);

CREATE INDEX IX_lead_data_adresse_email
ON dbo.lead_data(adresse_email);

CREATE INDEX IX_lead_data_campus
ON dbo.lead_data(campus);

CREATE INDEX IX_sync_logs_lead_id
ON dbo.sync_logs(lead_id);

CREATE INDEX IX_sync_logs_status
ON dbo.sync_logs(status);

CREATE INDEX IX_sync_logs_attempted_at
ON dbo.sync_logs(attempted_at);

