-- Migration 005: Advanced Pricing (Services and Speed Coefficients)

-- 1. Create services table
CREATE TABLE IF NOT EXISTS `services` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name_en` varchar(255) NOT NULL,
    `name_ru` varchar(255) DEFAULT NULL,
    `name_ua` varchar(255) DEFAULT NULL,
    `desc_en` text DEFAULT NULL,
    `tech_type` varchar(50) DEFAULT 'fdm',
    `price_per_hour` decimal(10,2) DEFAULT 0.00,
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2. Add price_per_hour, speed_coeff and service_id to materials
-- Using ALTER TABLE with multiple ADD COLUMN for efficiency
-- Note: IGNORE or existence check is handled by checking if column exists (manually or via script)
-- But here we just write the ALTER statements.

ALTER TABLE `materials` 
ADD COLUMN IF NOT EXISTS `price_per_hour` decimal(10,2) DEFAULT NULL,
ADD COLUMN IF NOT EXISTS `speed_coeff` decimal(10,2) DEFAULT 1.00,
ADD COLUMN IF NOT EXISTS `service_id` int(11) DEFAULT NULL;

-- 3. Initial seed for services if empty
INSERT INTO `services` (`name_en`, `name_ru`, `name_ua`, `tech_type`, `price_per_hour`)
SELECT 'FDM Printing', 'FDM Печать', 'FDM Друк', 'fdm', 2.00
WHERE NOT EXISTS (SELECT 1 FROM `services` WHERE `tech_type` = 'fdm');

INSERT INTO `services` (`name_en`, `name_ru`, `name_ua`, `tech_type`, `price_per_hour`)
SELECT 'SLA Printing', 'SLA Печать', 'SLA Друк', 'sla', 5.00
WHERE NOT EXISTS (SELECT 1 FROM `services` WHERE `tech_type` = 'sla');
