CREATE TABLE IF NOT EXISTS `equipment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(11) DEFAULT NULL,
  `code` varchar(50) DEFAULT NULL,
  `asset_tag` varchar(80) DEFAULT NULL,
  `type` varchar(80) DEFAULT 'Impressora',
  `manufacturer` varchar(120) DEFAULT NULL,
  `model` varchar(160) DEFAULT NULL,
  `serial_number` varchar(160) DEFAULT NULL,
  `location` varchar(180) DEFAULT NULL,
  `department` varchar(120) DEFAULT NULL,
  `contract_id` int(11) DEFAULT NULL,
  `status` enum('ativo','inativo','manutencao','baixado') DEFAULT 'ativo',
  `counter_bw` int(11) DEFAULT 0,
  `counter_color` int(11) DEFAULT 0,
  `fuser_life` int(11) DEFAULT NULL,
  `drum_life` int(11) DEFAULT NULL,
  `photo` varchar(255) DEFAULT NULL,
  `notes` text DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_equipment_customer` (`customer_id`),
  KEY `idx_equipment_contract` (`contract_id`),
  KEY `idx_equipment_code` (`code`),
  KEY `idx_equipment_serial` (`serial_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `customer_id` int(11) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `code` varchar(50) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `asset_tag` varchar(80) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `type` varchar(80) DEFAULT 'Impressora';
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `manufacturer` varchar(120) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `model` varchar(160) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `serial_number` varchar(160) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `location` varchar(180) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `department` varchar(120) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `contract_id` int(11) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `status` enum('ativo','inativo','manutencao','baixado') DEFAULT 'ativo';
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `counter_bw` int(11) DEFAULT 0;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `counter_color` int(11) DEFAULT 0;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `fuser_life` int(11) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `drum_life` int(11) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `photo` varchar(255) DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `notes` text DEFAULT NULL;
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `created_at` datetime DEFAULT current_timestamp();
ALTER TABLE `equipment` ADD COLUMN IF NOT EXISTS `updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp();

INSERT INTO `equipment` (`customer_id`,`code`,`asset_tag`,`type`,`manufacturer`,`model`,`serial_number`,`location`,`department`,`status`,`counter_bw`,`counter_color`,`fuser_life`,`drum_life`,`notes`)
SELECT NULL,'IMP-0001','SF-IMP-0001','Impressora','Ricoh','IM C3000','DEMO-123456','Cliente Demonstração','Financeiro','ativo',245851,38942,78,62,'Equipamento de demonstração'
WHERE NOT EXISTS (SELECT 1 FROM `equipment` WHERE `code`='IMP-0001');
