<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\ServiceItemFeature;
use App\Models\ServiceItem;
use App\Models\FeatureType;
use Illuminate\Support\Str;

class ServiceItemFeatureSeeder extends Seeder
{
    public function run(): void
    {
        // Get all service items and feature types
        $serviceItems = ServiceItem::all();
        $brandingType = FeatureType::where('slug', 'branding-type')->first();
        $laminationType = FeatureType::where('slug', 'lamination-type')->first();
        $bindingType = FeatureType::where('slug', 'binding-type')->first();
        $cuttingType = FeatureType::where('slug', 'cutting-type')->first();

        // Assign branding features to item branding items
        $brandingItems = ServiceItem::whereHas('serviceCategory', function($query) {
            $query->where('slug', 'item-branding');
        })->get();

        foreach ($brandingItems as $index => $item) {
            ServiceItemFeature::updateOrCreate(
                [
                    'service_item_id' => $item->id,
                    'feature_type_id' => $brandingType->id,
                ],
                [
                    'is_required' => false,
                    'sort_order' => 1,
                ]
            );
        }

        // Assign printing finish features to printing items
        $printingItems = ServiceItem::whereHas('serviceCategory', function($query) {
            $query->where('slug', 'printing');
        })->get();

        foreach ($printingItems as $index => $item) {
            // Add lamination type
            ServiceItemFeature::updateOrCreate(
                [
                    'service_item_id' => $item->id,
                    'feature_type_id' => $laminationType->id,
                ],
                [
                    'is_required' => false,
                    'sort_order' => 1,
                ]
            );

            // Add binding type (only for items that might need binding)
            if (in_array($item->slug, ['jotter-hard-cover'])) {
                ServiceItemFeature::updateOrCreate(
                    [
                        'service_item_id' => $item->id,
                        'feature_type_id' => $bindingType->id,
                    ],
                    [
                        'is_required' => false,
                        'sort_order' => 2,
                    ]
                );
            }

            // Add cutting type
            ServiceItemFeature::updateOrCreate(
                [
                    'service_item_id' => $item->id,
                    'feature_type_id' => $cuttingType->id,
                ],
                [
                    'is_required' => false,
                    'sort_order' => 3,
                ]
            );
        }
    }
}
