<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Product;
use Illuminate\Database\Seeder;

class RepairSeeder extends Seeder
{
    public function run(): void
    {
        $repairCategory = Category::where('slug', 'repair-fixation')->first();

        // 1. Create a Part (Physical Item)
        $screenPart = Product::create([
            'category_id' => $repairCategory->id ?? 1,
            'name' => 'iPhone 13 Screen',
            'name_ar' => 'شاشة ايفون 13',
            'sku' => 'PART-IP13-SCR',
            'barcode' => 'PART001',
            'barcode_type' => 'C128',
            'cost_price' => 1200, // EGP
            'selling_price' => 1800,
            'min_selling_price' => 1600,
            'is_repair_part' => true,
            'track_inventory' => true,
            'is_active' => true,
            'description' => 'Original OLED Screen for iPhone 13',
            'warranty_period' => 3,
        ]);

        // Add initial stock for the part (assuming 10 units)
        // Note: In real app, stock is added via Purchase Order or Adjustment
        // For seeding, we'll manually create inventory if Model supports it
        // Or leave it 0 and let user add stock? 
        // User complained about "nothing I can see", so let's add stock.
        // But Product.php doesn't have createStock method shown.
        // Assuming Inventory model usage.
        
        \App\Models\Inventory::create([
            'product_id' => $screenPart->id,
            'branch_id' => 1,
            'quantity' => 10,
            'location' => 'Shelf A1'
        ]);

        // 2. Create a Service (Labor) - Linked to the Part
        Product::create([
            'category_id' => $repairCategory->id ?? 1,
            'name' => 'iPhone 13 Screen Replacement Service',
            'name_ar' => 'خدمة تغيير شاشة ايفون 13',
            'sku' => 'SVC-IP13-SCR',
            'barcode' => 'SVC001',
            'barcode_type' => 'C128',
            'cost_price' => 0, // Labor has no direct COGS in this model usually
            'selling_price' => 200, // Service Fee
            'min_selling_price' => 150,
            'is_repair_part' => false,
            'is_service' => true,
            'track_inventory' => false, // Service, infinite stock
            'linked_product_id' => $screenPart->id, // Bundled Part
            'is_active' => true,
            'description' => 'Labor charge for screen replacement + Part automatically added',
        ]);
        
        // 3. Standalone Service (Cleaning)
        Product::create([
            'category_id' => $repairCategory->id ?? 1,
            'name' => 'Device Cleaning Service',
            'name_ar' => 'خدمة تنظيف الجهاز',
            'sku' => 'SVC-CLEAN',
            'barcode' => 'SVC002',
            'selling_price' => 100,
            'cost_price' => 0,
            'track_inventory' => false,
            'is_service' => true,
            'is_active' => true,
        ]);
    }
}
