<?php

namespace App\Http\Controllers\Api\V1\Backend\Catalog;

use App\Http\Controllers\Api\V1\Backend\ResponseController;
use App\Http\Controllers\Controller;
use App\Models\Backend\Catalog\Filter;
use App\Models\Backend\Catalog\FilterDescription;
use App\Models\Backend\Catalog\FilterGroup;
use App\Models\Backend\Catalog\FilterGroupDescription;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request;

class FilterController extends ResponseController
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        try {
            // dd('This is the index method of FilterController');
            $filters = FilterGroup::with(['descriptions'])
                ->get();
            // Return the response with the filters
            if ($filters->isEmpty()) {
                return $this->sendError('No filters found', [], 404);
            }

            return $this->sendResponse(
                $filters,
                'Filters retrieved successfully'
            );
        } catch (\Exception $e) {
            return $this->sendError('Failed to retrieve filters', [], 500);
        }
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'group_name' => 'required|string|max:255',
            'sort_order' => 'nullable|integer',
        ]);
        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }
        $filter = FilterGroup::create([
            'sort_order' => $request->input('sort_order', 0),
        ]);

        FilterGroupDescription::create([
            'filter_group_id' => $filter->filter_group_id,
            'language_id' => 1, // Assuming language_id 1 is default
            'name' => $request->group_name
        ]);
        
        foreach ($request->input('filter_values', []) as $value) {
            if (isset($value['name']) && !empty($value['name'])) {
                $filterValue = Filter::create([
                    'filter_group_id' => $filter->filter_group_id,
                    'sort_order' => $value['sort_order'] ?? 0,
                ]);

                FilterDescription::create([
                    'filter_id' => $filterValue->filter_id,
                    'language_id' => 1, // Assuming language_id 1 is default
                    'filter_group_id' => $filter->filter_group_id,
                    'name' => $value['name']
                ]);
            }
        }
        return $this->sendResponse(
            $filter,
            'Filter created successfully',
            201
        );
        try {
        } catch (\Exception $e) {
            return $this->sendError('Failed to create filter', [], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
