-- Script SQL para criar as tabelas manualmente
-- Execute este script no phpMyAdmin ou via linha de comando MySQL

USE figener_lic;

-- Tabela grupo (deve ser criada primeiro por causa da foreign key)
CREATE TABLE IF NOT EXISTS `grupo` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `descricao` varchar(255) NOT NULL,
  `volumes` int(11) NOT NULL DEFAULT 0,
  `ativo` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tabela cliente
CREATE TABLE IF NOT EXISTS `cliente` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `area` varchar(255) DEFAULT NULL,
  `nome` varchar(255) NOT NULL,
  `login` varchar(255) NOT NULL,
  `chave` varchar(255) NOT NULL,
  `ativo` tinyint(1) NOT NULL DEFAULT 1,
  `entrada` datetime DEFAULT NULL,
  `saida` datetime DEFAULT NULL,
  `ipconex` varchar(255) DEFAULT NULL,
  `mac_addr` varchar(255) DEFAULT NULL,
  `id_grupo` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `cliente_chave_unique` (`chave`),
  KEY `cliente_id_grupo_foreign` (`id_grupo`),
  CONSTRAINT `cliente_id_grupo_foreign` FOREIGN KEY (`id_grupo`) REFERENCES `grupo` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tabela log
CREATE TABLE IF NOT EXISTS `log` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `chave` varchar(255) NOT NULL,
  `ipconex` varchar(255) DEFAULT NULL,
  `data` datetime NOT NULL,
  `movimento` int(11) NOT NULL,
  `mac_addr` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `log_chave_index` (`chave`),
  KEY `log_data_index` (`data`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tabela uc_usuario
CREATE TABLE IF NOT EXISTS `uc_usuario` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `tipo` int(11) NOT NULL,
  `nome` varchar(255) NOT NULL,
  `login` varchar(255) NOT NULL,
  `email` varchar(255) DEFAULT NULL,
  `senha` varchar(255) NOT NULL,
  `parent_id` bigint(20) unsigned DEFAULT NULL,
  `previlegiado` tinyint(1) NOT NULL DEFAULT 0,
  `exclusivo` tinyint(1) NOT NULL DEFAULT 0,
  `ativo` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uc_usuario_login_unique` (`login`),
  KEY `uc_usuario_parent_id_foreign` (`parent_id`),
  CONSTRAINT `uc_usuario_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `uc_usuario` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tabela uc_modulo
CREATE TABLE IF NOT EXISTS `uc_modulo` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `tipo` int(11) NOT NULL,
  `grupo` varchar(255) NOT NULL,
  `alias` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uc_modulo_alias_unique` (`alias`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tabela uc_permissao
CREATE TABLE IF NOT EXISTS `uc_permissao` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `modulo_id` bigint(20) unsigned NOT NULL,
  `tipo_user` int(11) NOT NULL,
  `ativo` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uc_permissao_usuario_id_modulo_id_unique` (`usuario_id`,`modulo_id`),
  KEY `uc_permissao_modulo_id_foreign` (`modulo_id`),
  CONSTRAINT `uc_permissao_modulo_id_foreign` FOREIGN KEY (`modulo_id`) REFERENCES `uc_modulo` (`id`) ON DELETE CASCADE,
  CONSTRAINT `uc_permissao_usuario_id_foreign` FOREIGN KEY (`usuario_id`) REFERENCES `uc_usuario` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tabela migrations (para compatibilidade com Laravel)
CREATE TABLE IF NOT EXISTS `migrations` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `migration` varchar(255) NOT NULL,
  `batch` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
