# Quick Start: Checklist Inheritance Feature

## 🚀 5-Minute Setup & Test

### Prerequisites
- ✅ Migration has been run
- ✅ API server is running
- ✅ You have a valid authentication token

---

## Step 1: Import Postman Collection (30 seconds)

1. Open Postman
2. Import `postman_collection_maintenance_checklists.json`
3. Set collection variables:
   - `base_url`: Your API URL (e.g., `http://localhost/api`)
   - `access_token`: Your bearer token

---

## Step 2: Test Basic Inheritance (2 minutes)

### A. Create Parent Asset Type

```bash
POST {{base_url}}/maintenance/asset-types
```

```json
{
    "name": "HVAC Equipment",
    "code": "HVAC-001",
    "is_active": true
}
```

**Note the returned ID** (e.g., ID = 1)

---

### B. Create Child Asset Type

```bash
POST {{base_url}}/maintenance/asset-types
```

```json
{
    "name": "Air Conditioner",
    "code": "AC-001",
    "parent_id": 1,  // ID from step A
    "is_active": true
}
```

**Note the returned ID** (e.g., ID = 2)

---

### C. Create Parent Checklist

```bash
POST {{base_url}}/maintenance/checklists
```

```json
{
    "title": "General HVAC Checklist",
    "maintenance_project_id": 1,
    "maintenance_asset_type_id": 1,  // Parent asset type
    "is_active": true,
    "items": [
        {
            "question_text": "Check power supply",
            "question_type": "yes_no",
            "is_required": true,
            "order_position": 1
        }
    ]
}
```

---

### D. Create Child Checklist

```bash
POST {{base_url}}/maintenance/checklists
```

```json
{
    "title": "AC Specific Checklist",
    "maintenance_project_id": 1,
    "maintenance_asset_type_id": 2,  // Child asset type
    "is_active": true,
    "items": [
        {
            "question_text": "Check refrigerant",
            "question_type": "yes_no",
            "is_required": true,
            "order_position": 1
        }
    ]
}
```

---

### E. Test Inheritance ⭐

```bash
GET {{base_url}}/maintenance/checklists/asset-type/2/with-inheritance
```

**Expected Result:** You should receive **BOTH** checklists:
1. AC Specific Checklist (from child)
2. General HVAC Checklist (from parent)

✅ **Inheritance is working!**

---

## Step 3: Test Multi-Level Hierarchy (2 minutes)

### A. Create Grandchild Asset Type

```bash
POST {{base_url}}/maintenance/asset-types
```

```json
{
    "name": "Central AC",
    "code": "CAC-001",
    "parent_id": 2,  // Child asset type ID
    "is_active": true
}
```

**Note the ID** (e.g., ID = 3)

---

### B. Create Grandchild Checklist

```bash
POST {{base_url}}/maintenance/checklists
```

```json
{
    "title": "Central AC Checklist",
    "maintenance_project_id": 1,
    "maintenance_asset_type_id": 3,  // Grandchild asset type
    "is_active": true,
    "items": [
        {
            "question_text": "Check duct work",
            "question_type": "yes_no",
            "is_required": true,
            "order_position": 1
        }
    ]
}
```

---

### C. Test 3-Level Inheritance

```bash
GET {{base_url}}/maintenance/checklists/asset-type/3/with-inheritance
```

**Expected Result:** You should receive **ALL 3** checklists:
1. Central AC Checklist (from grandchild)
2. AC Specific Checklist (from child)
3. General HVAC Checklist (from parent)

✅ **Multi-level inheritance is working!**

---

## Visual Hierarchy

```
HVAC Equipment (ID: 1)
  └─ Checklist: "General HVAC Checklist"
      ├─ Check power supply

  └─ Air Conditioner (ID: 2)
      └─ Checklist: "AC Specific Checklist"
          ├─ Check refrigerant

      └─ Central AC (ID: 3)
          └─ Checklist: "Central AC Checklist"
              ├─ Check duct work
```

When you query Central AC (ID: 3) with inheritance:
```
GET /maintenance/checklists/asset-type/3/with-inheritance
```

You get ALL checklists from the hierarchy ⬆️

---

## Common Endpoints

### Without Inheritance (Direct Only)
```bash
GET /maintenance/checklists/asset-type/{id}
```
Returns only checklists directly assigned to that asset type.

### With Inheritance (Includes Parents)
```bash
GET /maintenance/checklists/asset-type/{id}/with-inheritance
```
Returns checklists from the asset type AND all parent types.

### With Inheritance + Project Filter
```bash
GET /maintenance/checklists/project/{projectId}/asset-type/{assetTypeId}/with-inheritance
```
Returns inherited checklists filtered by project.

---

## Verification Checklist

- [ ] Can create parent asset types
- [ ] Can create child asset types with `parent_id`
- [ ] Can create checklists for parent types
- [ ] Can create checklists for child types
- [ ] Inheritance endpoint returns parent checklists
- [ ] Inheritance endpoint returns child checklists
- [ ] Multi-level inheritance works (3+ levels)
- [ ] No duplicate checklists in results
- [ ] Only active checklists are returned

---

## Troubleshooting

### Not seeing parent checklists?

**Check:**
1. ✅ Using the correct endpoint: `/with-inheritance`
2. ✅ Parent asset type has `parent_id` set correctly
3. ✅ Parent checklist is active (`is_active: true`)
4. ✅ Parent checklist exists in database

### Getting duplicates?

**Check:**
- Checklists should auto-deduplicate by ID
- If seeing duplicates, there may be a data issue

### 404 Not Found?

**Check:**
1. ✅ Route file is included in `routes/apis/api.php`
2. ✅ Clear route cache: `php artisan route:clear`
3. ✅ Verify route exists: `php artisan route:list | grep checklist`

---

## Database Verification

Check the database directly:

```sql
-- View asset type hierarchy
SELECT
    id,
    name,
    parent_id,
    CASE WHEN parent_id IS NULL THEN 'ROOT' ELSE 'CHILD' END as level
FROM mod_maintenance_asset_types;

-- View checklists by asset type
SELECT
    c.id,
    c.title,
    c.maintenance_asset_type_id,
    at.name as asset_type_name,
    pat.name as parent_asset_type_name
FROM mod_maintenance_visit_checklists c
JOIN mod_maintenance_asset_types at ON c.maintenance_asset_type_id = at.id
LEFT JOIN mod_maintenance_asset_types pat ON at.parent_id = pat.id
WHERE c.is_active = 1;
```

---

## Next Steps

1. ✅ Test with your actual asset types
2. ✅ Create hierarchies for your equipment
3. ✅ Add checklists at appropriate levels
4. ✅ Test inheritance in your frontend
5. ✅ Train users on the new feature

---

## API Response Example

### Request
```http
GET /api/maintenance/checklists/asset-type/3/with-inheritance
Authorization: Bearer {token}
```

### Response
```json
{
    "success": true,
    "message": "قوائم التحقق لنوع الأصل مع الوراثة",
    "code": 200,
    "data": [
        {
            "id": 3,
            "title": "Central AC Checklist",
            "description": "Specific to central AC units",
            "maintenance_project_id": 1,
            "maintenance_asset_type_id": 3,
            "is_active": true,
            "items": [
                {
                    "id": 7,
                    "question_text": "Check duct work",
                    "question_type": "yes_no",
                    "is_required": true,
                    "order_position": 1
                }
            ]
        },
        {
            "id": 2,
            "title": "AC Specific Checklist",
            "description": "For all AC units",
            "maintenance_project_id": 1,
            "maintenance_asset_type_id": 2,
            "is_active": true,
            "items": [
                {
                    "id": 4,
                    "question_text": "Check refrigerant",
                    "question_type": "yes_no",
                    "is_required": true,
                    "order_position": 1
                }
            ]
        },
        {
            "id": 1,
            "title": "General HVAC Checklist",
            "description": "Common HVAC checks",
            "maintenance_project_id": 1,
            "maintenance_asset_type_id": 1,
            "is_active": true,
            "items": [
                {
                    "id": 1,
                    "question_text": "Check power supply",
                    "question_type": "yes_no",
                    "is_required": true,
                    "order_position": 1
                }
            ]
        }
    ]
}
```

Notice how the response includes checklists from:
- Asset Type 3 (Central AC) ← Direct
- Asset Type 2 (Air Conditioner) ← Parent
- Asset Type 1 (HVAC Equipment) ← Grandparent

---

## Summary

🎉 **You now have a working checklist inheritance system!**

Key Points:
- ✅ Asset types can have parent-child relationships
- ✅ Checklists inherit from parent asset types
- ✅ Supports unlimited hierarchy levels
- ✅ Automatic deduplication
- ✅ Backward compatible (old endpoints still work)

**Total Time:** ~5 minutes to set up and test ⚡

For more details, see: `MAINTENANCE_CHECKLISTS_API_GUIDE.md`
