-- V2 GÜNCELLEMESİ (Seviye, EXP, Maden ve Zanaat Sistemi)

-- 1. OYUNCU TABLOSU
ALTER TABLE `players` ADD COLUMN IF NOT EXISTS `exp` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `level`;

-- 2. BİNA TİPLERİNİ GÜNCELLE (mine ekle)
-- Hem definitions hem buildings tablolarına "mine" ekleniyor
ALTER TABLE `building_definitions` MODIFY COLUMN `type` ENUM('house','workplace','academy','mine') NOT NULL;
ALTER TABLE `buildings` MODIFY COLUMN `type` ENUM('house','workplace','academy','mine') NOT NULL;

-- 3. BİNALARIN DİNAMİK GELİRLERİ
-- Ne kadar hızda ne ürettiğini tutmak için resource türü ve son toplama tarihi
ALTER TABLE `building_definitions` ADD COLUMN IF NOT EXISTS `produce_resource` VARCHAR(30) NOT NULL DEFAULT 'money' AFTER `income_per_cycle`;
ALTER TABLE `buildings` ADD COLUMN IF NOT EXISTS `last_collected_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `cash`;

-- 4. EŞYA TANIMLARI (Üretim Bedeli Hammaddeleri eklendi ve Kategori Enum güncellendi)
ALTER TABLE `item_definitions` MODIFY COLUMN `category` ENUM('soldier','weapon','vehicle','spy','assassin','defense','tool','bomb','material') NOT NULL;

ALTER TABLE `item_definitions` 
  ADD COLUMN IF NOT EXISTS `req_plastic` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `produce_cost`,
  ADD COLUMN IF NOT EXISTS `req_copper`  INT UNSIGNED NOT NULL DEFAULT 0 AFTER `req_plastic`,
  ADD COLUMN IF NOT EXISTS `req_coal`    INT UNSIGNED NOT NULL DEFAULT 0 AFTER `req_copper`,
  ADD COLUMN IF NOT EXISTS `req_metal`   INT UNSIGNED NOT NULL DEFAULT 0 AFTER `req_coal`,
  ADD COLUMN IF NOT EXISTS `req_steel`   INT UNSIGNED NOT NULL DEFAULT 0 AFTER `req_metal`,
  ADD COLUMN IF NOT EXISTS `req_titanium` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `req_steel`;

-- 5. HAMMADDELERİ (Materyalleri) EKLE
INSERT IGNORE INTO `item_definitions` (`code`,`name`,`category`,`min_level`,`produce_time`,`produce_cost`) VALUES
('mat_plastic', 'Plastik', 'material', 1, 0, 0),
('mat_copper', 'Bakır', 'material', 2, 0, 0),
('mat_coal', 'Kömür', 'material', 3, 0, 0),
('mat_metal', 'Metal', 'material', 4, 0, 0),
('mat_steel', 'Çelik', 'material', 5, 0, 0),
('mat_titanium', 'Titanyum', 'material', 6, 0, 0);

-- 6. ESKİ DÜŞMANLARA HAMMADDE BEDELİ EKLE (Zorluk Dengesi)
UPDATE `item_definitions` SET `req_plastic`=10, `req_copper`=5 WHERE `code`='knife_man';
UPDATE `item_definitions` SET `req_metal`=20, `req_steel`=5 WHERE `code`='gunman';
UPDATE `item_definitions` SET `req_plastic`=50, `req_copper`=20 WHERE `code`='guard';
UPDATE `item_definitions` SET `req_titanium`=5, `req_steel`=10 WHERE `code`='spy';
UPDATE `item_definitions` SET `req_titanium`=20, `req_coal`=50 WHERE `code`='assassin';
UPDATE `item_definitions` SET `req_steel`=100, `req_metal`=250 WHERE `code`='car';
UPDATE `item_definitions` SET `req_steel`=300, `req_titanium`=50 WHERE `code`='truck';

-- 7. YENİ ASKERLER EKLENİYOR
INSERT IGNORE INTO `item_definitions` 
  (`code`,`name`,`category`,`min_level`,`attack`,`defense`,`agility`,`housing_cost`,`produce_time`,`produce_cost`, `req_metal`, `req_steel`) VALUES
('auto_gun', 'Otomatik Silahlı', 'soldier', 3, 40, 10, 15, 2, 240, 600, 30, 10),
('heavy_gun', 'Ağır Silahlı', 'soldier', 4, 70, 30, 5, 3, 400, 1200, 50, 30),
('sniper', 'Keskin Nişancı', 'soldier', 5, 120, 5, 40, 2, 600, 2000, 20, 50);

-- 8. MADEN / FABRİKALARIN TANIMLANMASI (Ana Kategoriler)
-- capacity: maks depolayabileceği hammadde miktarı
-- income_per_cycle: SAATLİK hammadde üretim hızı
INSERT IGNORE INTO `building_definitions` (`type`,`subtype_id`,`name`,`min_level`,`build_cost`,`build_time`,`capacity`,`income_per_cycle`,`produce_resource`) VALUES
('mine', 1, 'Plastik Geri Dönüşüm Tesisi', 1, 1000,  600,  300, 120, 'mat_plastic'),
('mine', 2, 'Bakır Madeni',               2, 3000,  1200, 250, 80,  'mat_copper'),
('mine', 3, 'Kömür Ocağı',                3, 7000,  2400, 200, 50,  'mat_coal'),
('mine', 4, 'Metal İşleme Fabrikası',     4, 15000, 4800, 150, 30,  'mat_metal'),
('mine', 5, 'Çelik Dökümhanesi',          5, 35000, 86400, 100, 15,  'mat_steel'),
('mine', 6, 'Titanyum Rafinerisi',        6, 80000, 172800,50,  5,   'mat_titanium');

-- (Örnek) Plastik Tesisi Yükseltme Seviyeleri (1. maden)
INSERT IGNORE INTO `building_level_defs` (`building_def_id`,`level`,`upgrade_cost`,`upgrade_time`,`capacity_bonus`,`income_bonus`)
SELECT bd.id, lv.level, lv.cost, lv.time, lv.cap_bonus, lv.inc_bonus
FROM `building_definitions` bd
JOIN (
  SELECT 2 AS level, 2000  AS cost, 1800 AS time, 100 AS cap_bonus, 50 AS inc_bonus UNION ALL
  SELECT 3,          5000,           3600,        250,              120 UNION ALL
  SELECT 4,          15000,          7200,        500,              200 UNION ALL
  SELECT 5,          40000,          14400,       1000,             400
) lv ON bd.subtype_id=1 AND bd.type='mine'
ON DUPLICATE KEY UPDATE upgrade_cost=VALUES(upgrade_cost);
