-- ============================================================
--  Migración 2026-10-15_03 · Documentos de la oferta
-- ------------------------------------------------------------
--  Archivos que sube el cotizador a una oferta (hojas de cálculo,
--  planos marcados, cotizaciones de proveedores, fotos...) para que quien
--  vea el proyecto después sepa cómo se cotizó. Se ven en Presupuesto
--  (pestaña Documentos de la oferta) y en Ventas (detalle de la oferta).
--  Los archivos van en storage/cotizaciones_venta/{id solicitud}/ (fuera
--  de public), igual que los adjuntos de la solicitud.
-- ============================================================
USE tinsa_db;

CREATE TABLE `cotizacion_venta_oferta_documento` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_oferta` int(10) unsigned NOT NULL,
  `id_cotizacion_venta` int(10) unsigned NOT NULL,
  `tipo` enum('imagen','documento','cad','otro') NOT NULL DEFAULT 'documento',
  `nombre_original` varchar(255) NOT NULL,
  `archivo` varchar(255) NOT NULL,
  `mime` varchar(120) DEFAULT NULL,
  `tamano` int(10) unsigned NOT NULL DEFAULT 0,
  `descripcion` varchar(255) DEFAULT NULL,
  `usuario_subio` int(10) unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_cvod_oferta` (`id_oferta`),
  KEY `fk_cvod_cotizacion` (`id_cotizacion_venta`),
  KEY `fk_cvod_usuario` (`usuario_subio`),
  CONSTRAINT `fk_cvod_oferta` FOREIGN KEY (`id_oferta`) REFERENCES `cotizacion_venta_oferta` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_cvod_cotizacion` FOREIGN KEY (`id_cotizacion_venta`) REFERENCES `cotizacion_venta` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_cvod_usuario` FOREIGN KEY (`usuario_subio`) REFERENCES `usuarios` (`id_usuario`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
