-- جدول فحوصات المركز الرئيسي
CREATE TABLE main_center_inspections (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    request_id VARCHAR(50) UNIQUE NOT NULL,
    inspection_type VARCHAR(50) NOT NULL,
    client_name VARCHAR(100) NOT NULL,
    client_phone VARCHAR(20) NOT NULL,
    vehicle_info JSONB NOT NULL,
    status VARCHAR(20) DEFAULT 'pending',
    assigned_by UUID REFERENCES users(id),
    assigned_at TIMESTAMP DEFAULT NOW(),
    completed_at TIMESTAMP,
    team_leader_id UUID REFERENCES users(id),
    total_sections INT DEFAULT 10,
    completed_sections INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT NOW()
);

-- جدول توزيع الفاحصين على الأقسام
CREATE TABLE inspector_assignments (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    inspection_id UUID REFERENCES main_center_inspections(id),
    inspector_id UUID REFERENCES users(id),
    section_name VARCHAR(100) NOT NULL,
    section_items JSONB, -- قائمة بنود القسم
    status VARCHAR(20) DEFAULT 'assigned',
    progress INT DEFAULT 0,
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    notes TEXT,
    UNIQUE(inspection_id, inspector_id, section_name)
);

-- جدول صلاحيات فاحصي المركز
CREATE TABLE inspector_permissions (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    inspector_id UUID REFERENCES users(id) UNIQUE,
    is_main_center BOOLEAN DEFAULT false,
    assigned_at TIMESTAMP DEFAULT NOW(),
    permissions JSONB, -- {"comprehensive": true, "dyno": false, "brakes": true}
    created_at TIMESTAMP DEFAULT NOW()
);

-- جدول سجل عمل الفريق
CREATE TABLE team_work_log (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    inspection_id UUID REFERENCES main_center_inspections(id),
    inspector_id UUID REFERENCES users(id),
    section_name VARCHAR(100),
    action VARCHAR(50),
    details JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);