-- ============================================
-- إنشاء جدول المستخدمين
-- AUTO TESTER - Users Table
-- ============================================

CREATE TABLE IF NOT EXISTS users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    full_name VARCHAR(100) NOT NULL,
    phone VARCHAR(20),
    role ENUM('admin', 'supervisor', 'inspector', 'cashier', 'client') DEFAULT 'client',
    
    -- صلاحيات المركز الرئيسي
    is_main_center BOOLEAN DEFAULT FALSE,
    
    -- الصورة الشخصية
    avatar_url VARCHAR(255),
    
    -- الحالة
    is_active BOOLEAN DEFAULT TRUE,
    last_login DATETIME,
    
    -- التواريخ
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    INDEX idx_role (role),
    INDEX idx_email (email),
    INDEX idx_phone (phone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================
-- إنشاء جدول صلاحيات المستخدمين
-- ============================================

CREATE TABLE IF NOT EXISTS user_permissions (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    
    -- أنواع الفحوصات المسموح بها
    can_comprehensive BOOLEAN DEFAULT FALSE,
    can_dyno BOOLEAN DEFAULT FALSE,
    can_brakes BOOLEAN DEFAULT FALSE,
    can_engine BOOLEAN DEFAULT FALSE,
    can_electrical BOOLEAN DEFAULT FALSE,
    can_external BOOLEAN DEFAULT FALSE,
    can_computer BOOLEAN DEFAULT FALSE,
    can_apps BOOLEAN DEFAULT FALSE,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    INDEX idx_user (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================
-- إدخال مستخدم افتراضي (مدير)
-- ============================================

-- كلمة المرور: admin123 (مشفرة)
INSERT INTO users (username, email, password_hash, full_name, phone, role, is_active) VALUES
('admin', 'admin@autotester.com', '$2a$10$XQ9JqJqJqJqJqJqJqJqJuO', 'مدير النظام', '01018518599', 'admin', TRUE);