CREATE TABLE clientes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nome VARCHAR(150) NOT NULL,
    email VARCHAR(150),
    whatsapp VARCHAR(30),
    criado_em DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE licencas (
    id INT AUTO_INCREMENT PRIMARY KEY,
    cliente_id INT NULL,
    chave VARCHAR(80) NOT NULL UNIQUE,
    tipo ENUM('teste','mensal','anual','vitalicia') NOT NULL DEFAULT 'mensal',
    status ENUM('ativa','expirada','suspensa','bloqueada') NOT NULL DEFAULT 'ativa',
    data_inicio DATE NOT NULL,
    data_expiracao DATE NULL,
    limite_maquinas INT NOT NULL DEFAULT 1,
    criado_em DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (cliente_id) REFERENCES clientes(id) ON DELETE SET NULL
);

CREATE TABLE ativacoes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    licenca_id INT NOT NULL,
    hardware_id VARCHAR(255) NOT NULL,
    nome_pc VARCHAR(150),
    usuario_windows VARCHAR(150),
    ativado_em DATETIME DEFAULT CURRENT_TIMESTAMP,
    ultimo_acesso DATETIME DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_pc_license (licenca_id, hardware_id),
    FOREIGN KEY (licenca_id) REFERENCES licencas(id) ON DELETE CASCADE
);

CREATE TABLE admin_users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nome VARCHAR(100) NOT NULL,
    email VARCHAR(150) NOT NULL UNIQUE,
    senha VARCHAR(255) NOT NULL,
    criado_em DATETIME DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO admin_users (nome, email, senha)
VALUES (
    'Administrador',
    'admin@promocreative.com.br',
    '$2y$10$y7NpkhOjQQczcDe1dW/p3.h7.x3JHxAw9vGzpvM3hKbYpUxh96iBC'
);