-- Migration: PKWT Scheduler Settings Configuration
-- Description: Konfigurasi scheduler untuk PKWT notifications per company
-- Date: 2026-02-06
-- Safe: Yes (menambahkan settings baru, tidak merusak data existing)

-- ============================================================================
-- KONFIGURASI PKWT SCHEDULER PER COMPANY
-- ============================================================================
-- 
-- Setting keys:
-- 1. pkwt_scheduler_enabled: boolean (true/false) - Enable/disable scheduler untuk company ini
-- 2. pkwt_scheduler_interval_hours: number - Interval waktu scheduler dalam jam (default: 24)
-- 3. pkwt_scheduler_days_before_expiration: number - Hari sebelum expiration untuk generate notifikasi (default: 30)
--
-- ============================================================================
-- CONTOH KONFIGURASI
-- ============================================================================

SET @company_id = 8; -- GANTI DENGAN COMPANY ID YANG INGIN DIKONFIGURASI
SET @modified_by = 1; -- User ID yang melakukan konfigurasi
SET @owner = 1; -- Owner ID

-- ============================================================================
-- Method 1: Using INSERT ... ON DUPLICATE KEY UPDATE (Recommended)
-- ============================================================================

-- Enable scheduler untuk company ini
INSERT INTO companysettings (company_id, `key`, value, creation, modified, modified_by, owner)
VALUES (
    @company_id,
    'pkwt_scheduler_enabled',
    'true', -- 'true' untuk enable, 'false' untuk disable
    NOW(),
    NOW(),
    @modified_by,
    @owner
)
ON DUPLICATE KEY UPDATE
    value = 'true',
    modified = NOW(),
    modified_by = @modified_by;

-- Set interval scheduler (dalam jam)
INSERT INTO companysettings (company_id, `key`, value, creation, modified, modified_by, owner)
VALUES (
    @company_id,
    'pkwt_scheduler_interval_hours',
    '24', -- Default: 24 jam (daily). Bisa diubah ke 12, 48, dll sesuai kebutuhan
    NOW(),
    NOW(),
    @modified_by,
    @owner
)
ON DUPLICATE KEY UPDATE
    value = '24',
    modified = NOW(),
    modified_by = @modified_by;

-- Set days before expiration untuk generate notifikasi
INSERT INTO companysettings (company_id, `key`, value, creation, modified, modified_by, owner)
VALUES (
    @company_id,
    'pkwt_scheduler_days_before_expiration',
    '30', -- Default: 30 hari. Bisa diubah ke 14, 45, 60, dll sesuai kebutuhan
    NOW(),
    NOW(),
    @modified_by,
    @owner
)
ON DUPLICATE KEY UPDATE
    value = '30',
    modified = NOW(),
    modified_by = @modified_by;

-- ============================================================================
-- VERIFIKASI KONFIGURASI
-- ============================================================================

-- Verify the settings were created/updated
-- Using BINARY to avoid collation mismatch issues (utf8mb4_general_ci vs utf8mb4_unicode_ci)
SELECT
    id,
    company_id,
    `key`,
    value,
    creation,
    modified,
    modified_by,
    owner
FROM companysettings
WHERE company_id = @company_id
AND BINARY `key` IN (
    BINARY 'pkwt_scheduler_enabled',
    BINARY 'pkwt_scheduler_interval_hours',
    BINARY 'pkwt_scheduler_days_before_expiration'
)
ORDER BY `key`;

-- ============================================================================
-- CONTOH KONFIGURASI LAINNYA
-- ============================================================================

-- Contoh 1: Disable scheduler untuk company tertentu
/*
INSERT INTO companysettings (company_id, `key`, value, creation, modified, modified_by, owner)
VALUES (
    @company_id,
    'pkwt_scheduler_enabled',
    'false',
    NOW(),
    NOW(),
    @modified_by,
    @owner
)
ON DUPLICATE KEY UPDATE
    value = 'false',
    modified = NOW(),
    modified_by = @modified_by;
*/

-- Contoh 2: Set interval menjadi 12 jam (2x sehari)
/*
INSERT INTO companysettings (company_id, `key`, value, creation, modified, modified_by, owner)
VALUES (
    @company_id,
    'pkwt_scheduler_interval_hours',
    '12',
    NOW(),
    NOW(),
    @modified_by,
    @owner
)
ON DUPLICATE KEY UPDATE
    value = '12',
    modified = NOW(),
    modified_by = @modified_by;
*/

-- Contoh 3: Set days before expiration menjadi 14 hari
/*
INSERT INTO companysettings (company_id, `key`, value, creation, modified, modified_by, owner)
VALUES (
    @company_id,
    'pkwt_scheduler_days_before_expiration',
    '14',
    NOW(),
    NOW(),
    @modified_by,
    @owner
)
ON DUPLICATE KEY UPDATE
    value = '14',
    modified = NOW(),
    modified_by = @modified_by;
*/

-- ============================================================================
-- CATATAN PENTING
-- ============================================================================
-- 
-- 1. Default Values:
--    - pkwt_scheduler_enabled: true (enabled by default)
--    - pkwt_scheduler_interval_hours: 24 (daily)
--    - pkwt_scheduler_days_before_expiration: 30 (30 days before expiration)
--
-- 2. Jika setting tidak ada, scheduler akan menggunakan default values
--
-- 3. Scheduler akan melewati company yang memiliki pkwt_scheduler_enabled = false
--
-- 4. Setiap company dapat memiliki konfigurasi yang berbeda:
--    - Company A: interval 24 jam, 30 hari sebelum expiration
--    - Company B: interval 12 jam, 14 hari sebelum expiration
--    - Company C: disabled (tidak akan diproses oleh scheduler)
--
-- 5. Konfigurasi ini dapat juga diatur melalui UI di halaman Company Settings > Notifications tab
--
-- ============================================================================
