
CREATE TABLE IF NOT EXISTS types_clients (
    id INT AUTO_INCREMENT PRIMARY KEY,
    libelle VARCHAR(80) NOT NULL,
    actif TINYINT(1) DEFAULT 1,
    date_creation DATETIME DEFAULT CURRENT_TIMESTAMP,
    date_modification DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO types_clients (libelle)
SELECT 'Hôtel' WHERE NOT EXISTS (SELECT 1 FROM types_clients WHERE libelle = 'Hôtel');
INSERT INTO types_clients (libelle)
SELECT 'Restaurant' WHERE NOT EXISTS (SELECT 1 FROM types_clients WHERE libelle = 'Restaurant');
INSERT INTO types_clients (libelle)
SELECT 'Café' WHERE NOT EXISTS (SELECT 1 FROM types_clients WHERE libelle = 'Café');
INSERT INTO types_clients (libelle)
SELECT 'Particulier' WHERE NOT EXISTS (SELECT 1 FROM types_clients WHERE libelle = 'Particulier');
INSERT INTO types_clients (libelle)
SELECT 'Entreprise' WHERE NOT EXISTS (SELECT 1 FROM types_clients WHERE libelle = 'Entreprise');
INSERT INTO types_clients (libelle)
SELECT 'Société de nettoyage' WHERE NOT EXISTS (SELECT 1 FROM types_clients WHERE libelle = 'Société de nettoyage');

CREATE TABLE IF NOT EXISTS clients (
    id INT AUTO_INCREMENT PRIMARY KEY,
    type_client_id INT NULL,
    code_client VARCHAR(20) NOT NULL UNIQUE,
    raison_sociale VARCHAR(150) NOT NULL,
    contact VARCHAR(150),
    adresse TEXT,
    code_postal VARCHAR(20),
    ville VARCHAR(100),
    pays VARCHAR(100) DEFAULT 'Maroc',
    telephone VARCHAR(30),
    mobile VARCHAR(30),
    email VARCHAR(150),
    site_web VARCHAR(150),
    ice VARCHAR(30),
    identifiant_fiscal VARCHAR(30),
    registre_commerce VARCHAR(30),
    commercial VARCHAR(100),
    remise DECIMAL(5,2) DEFAULT 0.00,
    mode_reglement VARCHAR(50),
    delai_paiement INT DEFAULT 0,
    plafond_credit DECIMAL(12,2) DEFAULT 0,
    observations TEXT,
    actif TINYINT(1) DEFAULT 1,
    date_creation DATETIME DEFAULT CURRENT_TIMESTAMP,
    date_modification DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_clients_type (type_client_id),
    INDEX idx_clients_raison (raison_sociale),
    INDEX idx_clients_ville (ville),
    FOREIGN KEY (type_client_id) REFERENCES types_clients(id)
);

CREATE TABLE IF NOT EXISTS clients_historique (
    id INT AUTO_INCREMENT PRIMARY KEY,
    client_id INT NOT NULL,
    action VARCHAR(50) NOT NULL,
    commentaire TEXT,
    utilisateur VARCHAR(100) DEFAULT 'Admin',
    date_action DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (client_id) REFERENCES clients(id)
);


