-- ============================================================
-- RENTACAR SISTEMA - Script SQL Completo
-- MySQL 8 - Generado automáticamente
-- ============================================================

SET FOREIGN_KEY_CHECKS = 0;
SET NAMES utf8mb4;

CREATE DATABASE IF NOT EXISTS `rentacar_db`
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE `rentacar_db`;

-- -------------------------------------------------------
-- SUCURSALES
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `sucursales` (
  `id`           BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `nombre`       VARCHAR(100) NOT NULL,
  `direccion`    VARCHAR(200),
  `ciudad`       VARCHAR(100),
  `region`       VARCHAR(100),
  `pais`         VARCHAR(80) DEFAULT 'Chile',
  `telefono`     VARCHAR(30),
  `email`        VARCHAR(150),
  `responsable`  VARCHAR(150),
  `activa`       TINYINT(1) DEFAULT 1,
  `deleted_at`   TIMESTAMP NULL,
  `created_at`   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`   TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- -------------------------------------------------------
-- USUARIOS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `usuarios` (
  `id`              BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `sucursal_id`     BIGINT UNSIGNED,
  `nombre`          VARCHAR(100) NOT NULL,
  `apellidos`       VARCHAR(100),
  `email`           VARCHAR(150) NOT NULL,
  `password`        VARCHAR(255) NOT NULL,
  `rol`             ENUM('administrador','recepcion','usuario') DEFAULT 'usuario',
  `activo`          TINYINT(1) DEFAULT 1,
  `intentos_login`  TINYINT DEFAULT 0,
  `bloqueado_hasta` DATETIME NULL,
  `ultimo_login`    DATETIME NULL,
  `remember_token`  VARCHAR(100) NULL,
  `deleted_at`      TIMESTAMP NULL,
  `created_at`      TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`      TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `usuarios_email_unique` (`email`),
  KEY `usuarios_sucursal_id_foreign` (`sucursal_id`),
  CONSTRAINT `usuarios_sucursal_id_foreign` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- -------------------------------------------------------
-- MONEDAS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `monedas` (
  `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `codigo`      VARCHAR(10) NOT NULL,
  `nombre`      VARCHAR(60) NOT NULL,
  `simbolo`     VARCHAR(5),
  `tipo_cambio` DECIMAL(12,4) DEFAULT 1.0000,
  `principal`   TINYINT(1) DEFAULT 0,
  `created_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `monedas_codigo_unique` (`codigo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- VEHÍCULOS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `vehiculos` (
  `id`               BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `sucursal_id`      BIGINT UNSIGNED NOT NULL,
  `moneda_id`        BIGINT UNSIGNED,
  `marca`            VARCHAR(80) NOT NULL,
  `modelo`           VARCHAR(80) NOT NULL,
  `anio`             YEAR NOT NULL,
  `color`            VARCHAR(50),
  `combustible`      ENUM('bencina','diesel','electrico','hibrido','gas') DEFAULT 'bencina',
  `tipo`             VARCHAR(60),
  `vin`              VARCHAR(50) NULL,
  `numero_motor`     VARCHAR(50),
  `pasajeros`        TINYINT DEFAULT 5,
  `transmision`      ENUM('manual','automatica') DEFAULT 'manual',
  `aire_acond`       TINYINT(1) DEFAULT 0,
  `kilometraje`      INT DEFAULT 0,
  `estado`           ENUM('disponible','reservado','arriendo','mantencion','fuera_servicio') DEFAULT 'disponible',
  `precio_diario`    DECIMAL(12,2) DEFAULT 0.00,
  `activo`           TINYINT(1) DEFAULT 1,
  `foto_principal`   VARCHAR(300),
  `campos_adicionales` JSON,
  `deleted_at`       TIMESTAMP NULL,
  `created_at`       TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`       TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `vehiculos_vin_unique` (`vin`),
  KEY `vehiculos_sucursal_id_foreign` (`sucursal_id`),
  KEY `vehiculos_estado_index` (`estado`),
  CONSTRAINT `vehiculos_sucursal_id_foreign` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `vehiculos_moneda_id_foreign` FOREIGN KEY (`moneda_id`) REFERENCES `monedas` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- -------------------------------------------------------
-- FOTOS Y DOCUMENTOS DE VEHÍCULOS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `vehiculos_fotos` (
  `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `vehiculo_id` BIGINT UNSIGNED NOT NULL,
  `ruta`        VARCHAR(300) NOT NULL,
  `orden`       TINYINT DEFAULT 0,
  `created_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT `vf_vehiculo_id_fk` FOREIGN KEY (`vehiculo_id`) REFERENCES `vehiculos` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `vehiculos_documentos` (
  `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `vehiculo_id` BIGINT UNSIGNED NOT NULL,
  `tipo`        VARCHAR(80),
  `nombre`      VARCHAR(150),
  `ruta`        VARCHAR(300),
  `fecha_venc`  DATE NULL,
  `alerta_dias` INT DEFAULT 30,
  `created_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT `vd_vehiculo_id_fk` FOREIGN KEY (`vehiculo_id`) REFERENCES `vehiculos` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `transferencias_vehiculos` (
  `id`                  BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `vehiculo_id`         BIGINT UNSIGNED NOT NULL,
  `usuario_id`          BIGINT UNSIGNED NOT NULL,
  `sucursal_origen_id`  BIGINT UNSIGNED NOT NULL,
  `sucursal_destino_id` BIGINT UNSIGNED NOT NULL,
  `fecha`               DATETIME NOT NULL,
  `observaciones`       TEXT,
  `created_at`          TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  KEY `tv_vehiculo_idx` (`vehiculo_id`),
  CONSTRAINT `tv_vehiculo_fk` FOREIGN KEY (`vehiculo_id`) REFERENCES `vehiculos` (`id`),
  CONSTRAINT `tv_usuario_fk`  FOREIGN KEY (`usuario_id`)  REFERENCES `usuarios` (`id`),
  CONSTRAINT `tv_origen_fk`   FOREIGN KEY (`sucursal_origen_id`)  REFERENCES `sucursales` (`id`),
  CONSTRAINT `tv_destino_fk`  FOREIGN KEY (`sucursal_destino_id`) REFERENCES `sucursales` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- CLIENTES
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `clientes` (
  `id`                  BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `nombre`              VARCHAR(100) NOT NULL,
  `apellidos`           VARCHAR(100),
  `rut`                 VARCHAR(20) NULL,
  `pasaporte`           VARCHAR(40) NULL,
  `direccion`           VARCHAR(200),
  `ciudad`              VARCHAR(100),
  `pais`                VARCHAR(80) DEFAULT 'Chile',
  `telefono`            VARCHAR(30),
  `email`               VARCHAR(150),
  `activo`              TINYINT(1) DEFAULT 1,
  `campos_adicionales`  JSON,
  `deleted_at`          TIMESTAMP NULL,
  `created_at`          TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`          TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY `clientes_rut_index` (`rut`),
  KEY `clientes_email_index` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `clientes_documentos` (
  `id`         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `cliente_id` BIGINT UNSIGNED NOT NULL,
  `tipo`       VARCHAR(80),
  `nombre`     VARCHAR(150),
  `ruta`       VARCHAR(300),
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT `cd_cliente_id_fk` FOREIGN KEY (`cliente_id`) REFERENCES `clientes` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- RESERVAS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `reservas` (
  `id`                BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `vehiculo_id`       BIGINT UNSIGNED NOT NULL,
  `cliente_id`        BIGINT UNSIGNED NULL,
  `sucursal_id`       BIGINT UNSIGNED NOT NULL,
  `usuario_id`        BIGINT UNSIGNED NULL,
  `moneda_id`         BIGINT UNSIGNED NULL,
  `fecha_inicio`      DATE NOT NULL,
  `fecha_fin`         DATE NOT NULL,
  `hora_inicio`       TIME NULL,
  `hora_fin`          TIME NULL,
  `estado`            ENUM('pendiente','confirmada','rechazada','cancelada','finalizada') DEFAULT 'pendiente',
  `origen`            ENUM('web','panel') DEFAULT 'panel',
  `observaciones`     TEXT,
  `valor_reserva`     DECIMAL(12,2) DEFAULT 0.00,
  `nombre_contacto`   VARCHAR(150),
  `email_contacto`    VARCHAR(150),
  `telefono_contacto` VARCHAR(30),
  `created_at`        TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`        TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY `reservas_vehiculo_idx` (`vehiculo_id`),
  KEY `reservas_estado_idx` (`estado`),
  KEY `reservas_fechas_idx` (`fecha_inicio`, `fecha_fin`),
  CONSTRAINT `r_vehiculo_fk`  FOREIGN KEY (`vehiculo_id`) REFERENCES `vehiculos` (`id`),
  CONSTRAINT `r_cliente_fk`   FOREIGN KEY (`cliente_id`)  REFERENCES `clientes` (`id`),
  CONSTRAINT `r_sucursal_fk`  FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- -------------------------------------------------------
-- CONTRATOS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `contratos` (
  `id`                 BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `numero`             VARCHAR(20) NOT NULL,
  `reserva_id`         BIGINT UNSIGNED NULL,
  `vehiculo_id`        BIGINT UNSIGNED NOT NULL,
  `cliente_id`         BIGINT UNSIGNED NOT NULL,
  `sucursal_id`        BIGINT UNSIGNED NOT NULL,
  `usuario_id`         BIGINT UNSIGNED NOT NULL,
  `moneda_id`          BIGINT UNSIGNED NULL,
  `fecha_salida`       DATETIME NOT NULL,
  `fecha_retorno`      DATETIME NOT NULL,
  `km_salida`          INT NOT NULL,
  `km_retorno`         INT NULL,
  `combustible_sal`    TINYINT DEFAULT 100,
  `combustible_ret`    TINYINT NULL,
  `garantia`           DECIMAL(12,2) DEFAULT 0.00,
  `valor_diario`       DECIMAL(12,2) NOT NULL,
  `total_dias`         INT NOT NULL,
  `subtotal`           DECIMAL(12,2) NOT NULL,
  `descuentos`         DECIMAL(12,2) DEFAULT 0.00,
  `total`              DECIMAL(12,2) NOT NULL,
  `estado`             ENUM('activo','devuelto','anulado') DEFAULT 'activo',
  `pdf_ruta`           VARCHAR(300) NULL,
  `observaciones`      TEXT,
  `inspeccion_salida`  JSON,
  `inspeccion_retorno` JSON,
  `created_at`         TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`         TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `contratos_numero_unique` (`numero`),
  KEY `contratos_vehiculo_idx` (`vehiculo_id`),
  KEY `contratos_estado_idx` (`estado`),
  CONSTRAINT `c_vehiculo_fk`  FOREIGN KEY (`vehiculo_id`) REFERENCES `vehiculos` (`id`),
  CONSTRAINT `c_cliente_fk`   FOREIGN KEY (`cliente_id`)  REFERENCES `clientes` (`id`),
  CONSTRAINT `c_sucursal_fk`  FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `c_usuario_fk`   FOREIGN KEY (`usuario_id`)  REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `contratos_cargos` (
  `id`                 BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `contrato_id`        BIGINT UNSIGNED NOT NULL,
  `concepto`           VARCHAR(200) NOT NULL,
  `descripcion`        TEXT,
  `valor`              DECIMAL(12,2) NOT NULL,
  `descontar_garantia` TINYINT(1) DEFAULT 1,
  `created_at`         TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT `cc_contrato_fk` FOREIGN KEY (`contrato_id`) REFERENCES `contratos` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- MANTENCIONES
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `mantenciones` (
  `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `vehiculo_id` BIGINT UNSIGNED NOT NULL,
  `usuario_id`  BIGINT UNSIGNED NULL,
  `tipo`        VARCHAR(100) NOT NULL,
  `descripcion` TEXT,
  `fecha`       DATE NOT NULL,
  `km_actual`   INT NULL,
  `km_proximo`  INT NULL,
  `fecha_prox`  DATE NULL,
  `costo`       DECIMAL(12,2) DEFAULT 0.00,
  `proveedor`   VARCHAR(150),
  `estado`      ENUM('pendiente','en_proceso','completada') DEFAULT 'pendiente',
  `created_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY `m_vehiculo_idx` (`vehiculo_id`),
  CONSTRAINT `m_vehiculo_fk` FOREIGN KEY (`vehiculo_id`) REFERENCES `vehiculos` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- -------------------------------------------------------
-- MEDIOS DE PAGO
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `medios_pago` (
  `id`                  BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `nombre`              VARCHAR(80) NOT NULL,
  `codigo`              VARCHAR(30) NOT NULL,
  `activo`              TINYINT(1) DEFAULT 1,
  `requiere_referencia` TINYINT(1) DEFAULT 0,
  `created_at`          TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `mp_codigo_unique` (`codigo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- PAGOS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `pagos` (
  `id`            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `contrato_id`   BIGINT UNSIGNED NULL,
  `caja_id`       BIGINT UNSIGNED NULL,
  `medio_pago_id` BIGINT UNSIGNED NULL,
  `moneda_id`     BIGINT UNSIGNED NULL,
  `usuario_id`    BIGINT UNSIGNED NULL,
  `monto`         DECIMAL(12,2) NOT NULL,
  `referencia`    VARCHAR(100),
  `tipo`          VARCHAR(50),
  `created_at`    TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- CAJA
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `cajas` (
  `id`            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `sucursal_id`   BIGINT UNSIGNED NOT NULL,
  `usuario_id`    BIGINT UNSIGNED NOT NULL,
  `fecha`         DATE NOT NULL,
  `saldo_inicial` BIGINT NOT NULL DEFAULT 0,
  `saldo_final`   BIGINT NULL,
  `estado`        ENUM('abierta','cerrada') DEFAULT 'abierta',
  `created_at`    TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at`    TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY `cajas_sucursal_fecha_idx` (`sucursal_id`, `fecha`),
  CONSTRAINT `cajas_sucursal_fk` FOREIGN KEY (`sucursal_id`) REFERENCES `sucursales` (`id`),
  CONSTRAINT `cajas_usuario_fk`  FOREIGN KEY (`usuario_id`)  REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `caja_movimientos` (
  `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `caja_id`     BIGINT UNSIGNED NOT NULL,
  `usuario_id`  BIGINT UNSIGNED NULL,
  `contrato_id` BIGINT UNSIGNED NULL,
  `tipo`        ENUM('ingreso','egreso','garantia_rec','garantia_dev','ajuste') NOT NULL,
  `concepto`    VARCHAR(200) NOT NULL,
  `monto`       BIGINT NOT NULL,
  `medio_pago`  VARCHAR(60),
  `referencia`  VARCHAR(100),
  `created_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  KEY `cm_caja_idx` (`caja_id`),
  CONSTRAINT `cm_caja_fk` FOREIGN KEY (`caja_id`) REFERENCES `cajas` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- AUDITORÍA
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `auditoria` (
  `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `usuario_id`  BIGINT UNSIGNED NULL,
  `accion`      VARCHAR(150) NOT NULL,
  `modulo`      VARCHAR(80),
  `registro_id` BIGINT UNSIGNED NULL,
  `detalle`     JSON,
  `ip`          VARCHAR(45),
  `user_agent`  VARCHAR(300),
  `created_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  KEY `aud_usuario_idx` (`usuario_id`),
  KEY `aud_modulo_idx` (`modulo`),
  KEY `aud_created_idx` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- CONFIGURACIONES
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `configuraciones` (
  `id`         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `clave`      VARCHAR(100) NOT NULL,
  `valor`      TEXT,
  `grupo`      VARCHAR(60) DEFAULT 'general',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `config_clave_unique` (`clave`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- RESET TOKENS
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `password_reset_tokens` (
  `email`      VARCHAR(150) NOT NULL PRIMARY KEY,
  `token`      VARCHAR(255) NOT NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- JOBS (para colas)
-- -------------------------------------------------------
CREATE TABLE IF NOT EXISTS `jobs` (
  `id`           BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `queue`        VARCHAR(255) NOT NULL,
  `payload`      LONGTEXT NOT NULL,
  `attempts`     TINYINT UNSIGNED NOT NULL,
  `reserved_at`  INT UNSIGNED NULL,
  `available_at` INT UNSIGNED NOT NULL,
  `created_at`   INT UNSIGNED NOT NULL,
  KEY `jobs_queue_index` (`queue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `failed_jobs` (
  `id`         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `uuid`       VARCHAR(255) NOT NULL UNIQUE,
  `connection` TEXT NOT NULL,
  `queue`      TEXT NOT NULL,
  `payload`    LONGTEXT NOT NULL,
  `exception`  LONGTEXT NOT NULL,
  `failed_at`  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- -------------------------------------------------------
-- DATOS INICIALES
-- -------------------------------------------------------
INSERT INTO `monedas` (`codigo`,`nombre`,`simbolo`,`tipo_cambio`,`principal`) VALUES
('CLP','Peso Chileno','$',1.0000,1),
('USD','Dólar Americano','US$',950.0000,0),
('EUR','Euro','€',1050.0000,0);

INSERT INTO `medios_pago` (`nombre`,`codigo`,`activo`,`requiere_referencia`) VALUES
('Efectivo',       'efectivo',      1, 0),
('Transferencia',  'transferencia', 1, 1),
('Débito',         'debito',        1, 1),
('Crédito',        'credito',       1, 1),
('WebPay',         'webpay',        0, 1),
('Mercado Pago',   'mercadopago',   0, 1);

INSERT INTO `configuraciones` (`clave`,`valor`,`grupo`) VALUES
('empresa_nombre',    'RentaCar',       'empresa'),
('empresa_rut',       '12.345.678-9',   'empresa'),
('empresa_direccion', 'Santiago, Chile','empresa'),
('empresa_telefono',  '+56 2 1234 5678','empresa'),
('empresa_email',     'info@rentacar.cl','empresa'),
('garantia_global',   '100000',         'operacion'),
('valor_reserva',     '50000',          'operacion'),
('impuesto_pct',      '19',             'operacion'),
('plantilla_contrato','<p>Contrato de arriendo entre {{empresa}} y {{cliente_nombre}} {{cliente_apellidos}}, RUT {{cliente_rut}}, para el vehículo {{vehiculo_marca}} {{vehiculo_modelo}} año {{vehiculo_anio}}, patente {{vehiculo_vin}}. Período: {{fecha_salida}} al {{fecha_retorno}}. Valor diario: {{moneda_simbolo}}{{valor_diario}}. Total: {{moneda_simbolo}}{{total}}. Garantía: {{moneda_simbolo}}{{garantia}}.</p>', 'contratos');

-- Sucursal y usuario administrador inicial
INSERT INTO `sucursales` (`nombre`,`ciudad`,`pais`,`activa`) VALUES
('Casa Matriz','Santiago','Chile',1);

-- Contraseña: Admin1234! (bcrypt)
INSERT INTO `usuarios` (`sucursal_id`,`nombre`,`apellidos`,`email`,`password`,`rol`,`activo`) VALUES
(1,'Administrador','Sistema','admin@rentacar.cl','$2y$12$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
'administrador',1);

SET FOREIGN_KEY_CHECKS = 1;

-- ============================================================
-- FIN DEL SCRIPT
-- ============================================================
