ALTER TABLE users
    ADD COLUMN IF NOT EXISTS phone varchar(40) NULL AFTER mail,
    ADD COLUMN IF NOT EXISTS address varchar(255) NULL AFTER phone,
    ADD COLUMN IF NOT EXISTS postcode varchar(30) NULL AFTER address,
    ADD COLUMN IF NOT EXISTS city varchar(120) NULL AFTER postcode,
    ADD COLUMN IF NOT EXISTS country varchar(120) NULL DEFAULT 'France' AFTER city,
    ADD COLUMN IF NOT EXISTS is_business tinyint(1) NOT NULL DEFAULT 0 AFTER country,
    ADD COLUMN IF NOT EXISTS company varchar(255) NULL AFTER is_business,
    ADD COLUMN IF NOT EXISTS vat varchar(120) NULL AFTER company,
    ADD COLUMN IF NOT EXISTS terms_accepted tinyint(1) NOT NULL DEFAULT 0 AFTER vat,
    ADD COLUMN IF NOT EXISTS ban tinyint(1) NOT NULL DEFAULT 0 AFTER staff,
    ADD COLUMN IF NOT EXISTS ban_raison varchar(255) NULL AFTER ban;

CREATE TABLE IF NOT EXISTS user_last_login (
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL,
    ip varchar(255) NOT NULL,
    user_agent text NULL,
    date varchar(255) NOT NULL,
    PRIMARY KEY (id),
    KEY idx_user_last_login_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

SET @mail_index_count = (
    SELECT COUNT(1)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'users'
      AND index_name = 'uniq_users_mail'
);
SET @mail_index_sql = IF(@mail_index_count = 0, 'ALTER TABLE users ADD UNIQUE KEY uniq_users_mail (mail)', 'SELECT 1');
PREPARE stmt FROM @mail_index_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
