-- =============================================
-- PEPC V2 - ADMİN PANEL VERİTABANI ŞEMASI
-- =============================================
-- NOT: Mevcut tablolar güncellenmeyecek, sadece eksik tablolar oluşturulacak

-- =============================================
-- 1. SİSTEM LOGLARI
-- =============================================
CREATE TABLE IF NOT EXISTS system_logs (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NULL,
    action VARCHAR(100) NOT NULL,
    entity_type VARCHAR(50) NULL,
    entity_id INT NULL,
    old_value TEXT NULL,
    new_value TEXT NULL,
    ip_address VARCHAR(45) NULL,
    user_agent VARCHAR(500) NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL,
    INDEX idx_action (action),
    INDEX idx_entity (entity_type, entity_id),
    INDEX idx_date (created_at)
);

-- =============================================
-- 2. SENKRONIZASYON DURUMU
-- =============================================
CREATE TABLE IF NOT EXISTS sync_status (
    id INT PRIMARY KEY AUTO_INCREMENT,
    sync_type ENUM('full', 'partial', 'manual') NOT NULL,
    status ENUM('running', 'completed', 'failed') NOT NULL,
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP NULL,
    files_scanned INT DEFAULT 0,
    files_added INT DEFAULT 0,
    files_updated INT DEFAULT 0,
    files_removed INT DEFAULT 0,
    categories_added INT DEFAULT 0,
    error_message TEXT NULL,
    started_by INT NULL,
    FOREIGN KEY (started_by) REFERENCES users(id) ON DELETE SET NULL,
    INDEX idx_status (status),
    INDEX idx_started_at (started_at)
);

-- =============================================
-- 3. TEST VERİLERİ
-- =============================================

-- Test kullanıcıları (bazıları zaten var, hata verebilir - sorun değil)
INSERT IGNORE INTO users (username, email, password_hash, role, is_active) VALUES 
('admin', 'admin@pepc.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin', 1),
('osman', 'osman@pepc.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'user', 1),
('test_user', 'test@pepc.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'user', 1);

-- Test bakiyeleri
INSERT IGNORE INTO user_balances (user_id, balance) 
SELECT id, 100.00 FROM users WHERE username = 'osman';

INSERT IGNORE INTO user_balances (user_id, balance) 
SELECT id, 50.00 FROM users WHERE username = 'test_user';
