USE contabilidade_core;

CREATE TABLE IF NOT EXISTS users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(120) NOT NULL,
    email VARCHAR(160) NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    role ENUM('admin','accountant') NOT NULL DEFAULT 'accountant',
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    last_login_at TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_users_email (email),
    KEY idx_users_active (is_active)
) ENGINE=InnoDB;

INSERT INTO users (name, email, password_hash, role, is_active)
VALUES ('Administrador', 'admin@contabilidade.local', '$2y$10$3pAhWYifPhHl1Dik.7d2V.m2h2zl99dV0wwMcp91XbvhiU3mBJHwe', 'admin', 1)
ON DUPLICATE KEY UPDATE email = email;
