-- =====================================================
-- SERVER UPDATE SCRIPT
-- Jalankan di phpMyAdmin satu per satu
-- =====================================================

-- =====================================================
-- 1. CREATE user_devices TABLE (untuk device validation)
-- =====================================================
CREATE TABLE IF NOT EXISTS `user_devices` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `device_id` varchar(255) NOT NULL,
  `device_model` varchar(255) DEFAULT NULL,
  `device_brand` varchar(255) DEFAULT NULL,
  `os_version` varchar(255) DEFAULT NULL,
  `app_version` varchar(255) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `registered_at` timestamp NULL DEFAULT NULL,
  `last_used_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_devices_user_id_device_id_unique` (`user_id`, `device_id`),
  KEY `user_devices_user_id_index` (`user_id`),
  KEY `user_devices_device_id_index` (`device_id`),
  CONSTRAINT `user_devices_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =====================================================
-- 2. ADD device_validation_enabled TO companies
-- =====================================================
ALTER TABLE `companies`
ADD COLUMN IF NOT EXISTS `device_validation_enabled` TINYINT(1) NOT NULL DEFAULT 0
AFTER `attendance_auto_enabled`;


-- =====================================================
-- 3. ADD is_primary TO locations (jika belum ada)
-- =====================================================
ALTER TABLE `locations`
ADD COLUMN IF NOT EXISTS `is_primary` TINYINT(1) NOT NULL DEFAULT 0
AFTER `is_active`;

-- Add index for is_primary
ALTER TABLE `locations`
ADD INDEX IF NOT EXISTS `locations_is_primary_index` (`is_primary`);


-- =====================================================
-- 4. ADD is_primary TO business_trip_locations (jika belum ada)
-- =====================================================
ALTER TABLE `business_trip_locations`
ADD COLUMN IF NOT EXISTS `is_primary` TINYINT(1) NOT NULL DEFAULT 0
AFTER `is_active`;


-- =====================================================
-- 5. ADD sort_order TO locations (jika belum ada)
-- =====================================================
ALTER TABLE `locations`
ADD COLUMN IF NOT EXISTS `sort_order` INT NOT NULL DEFAULT 0
AFTER `is_primary`;


-- =====================================================
-- 6. Verify - Jalankan setelah semua ALTER selesai
-- =====================================================
-- SELECT 'user_devices' as tbl, COUNT(*) as cnt FROM user_devices
-- UNION ALL
-- SELECT 'Check device_validation_enabled', COUNT(*) FROM information_schema.COLUMNS
-- WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'companies' AND COLUMN_NAME = 'device_validation_enabled'
-- UNION ALL
-- SELECT 'Check is_primary in locations', COUNT(*) FROM information_schema.COLUMNS
-- WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'locations' AND COLUMN_NAME = 'is_primary';

