<?php

namespace App\Http\Controllers\Api\Admin\Silver;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Visit;
use App\Models\Currency;
use App\Models\GoldPrice;
use App\Traits\ApiResponder;
use Illuminate\Http\Request;
use App\Models\CurrencyPrice;
use App\Support\FileUploader;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Visit\StoreVisitRequest;
use App\Http\Resources\Admin\Gold\GoldIndexResource;
use App\Http\Requests\Admin\Visit\UpdateVisitRequest;
use App\Http\Resources\Admin\Visit\VisitShowResource;
use App\Http\Resources\Admin\Visit\VisitIndexResource;
use App\Http\Requests\Admin\Currency\UpdateCurrencyRequest;
use App\Http\Requests\Admin\Gold\UpdateGoldRequest;
use App\Http\Resources\Admin\Currency\CurrencyIndexResource;
use App\Http\Resources\Admin\Silver\SilverIndexResource as SilverSilverIndexResource;
use App\Http\Resources\Silvers\SilverIndexResource;
use App\Models\Country;
use App\Models\SilverPrice;

class SilverController extends Controller
{
    use ApiResponder;
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $country_id = $request->get('country_id', 1);
        $search = $request->get('search');

        $silvers = SilverPrice::with(['silver', 'country'])

            // ✅ فلترة country فقط لو مفيش search
            ->when(!$search, function ($query) use ($country_id) {
                $query->where('country_id', $country_id);
            })

            // ✅ search في كل الدول
            ->when($search, function ($query) use ($search, $request) {
                $lang = $request->header('lang', 'ar');

                $query->where(function ($q) use ($search, $lang) {

                    // 🔍 search في silver (مثلاً: 999 / 925)
                    $q->whereHas('silver', function ($q2) use ($search, $lang) {
                        $q2->where("name_{$lang}", 'like', "%{$search}%");
                    })

                        // 🔍 search في country
                        ->orWhereHas('country', function ($q2) use ($search, $lang) {
                            $q2->where("name_{$lang}", 'like', "%{$search}%");
                        });
                });
            })

            ->orderBy('country_id', 'asc')
            ->orderBy('ordering', 'asc')
            ->get();

        return $this->respondResource(
            SilverSilverIndexResource::collection($silvers)
        );
    }

    /**
     * Store a newly created resource in storage.
     */
    // public function store(StoreVisitRequest $request)
    // {
    //     $data = $request->except(['images', 'name', 'phone']);
    //     // dd($data);
    //     $data['day'] = Carbon::parse($request->date_time)->toDateString();
    //     $data['time'] = Carbon::parse($request->date_time)->format('H:i:s');

    //     $user = User::where('phone', $request->phone)->first();
    //     if (!$user) {
    //         $user = User::create([
    //             'name' => $request->name,
    //             'phone' => $request->phone,
    //         ]);
    //     }
    //     $data['user_id'] = $user->id;

    //     $new_visit = Visit::create($data);

    //     if ($request->hasFile('images')) {
    //         foreach ($request->images as $image) {
    //             $newImage = (new FileUploader())->save($image, 'visits');
    //             // dd($newImage);
    //             $new_visit->files()->create([
    //                 'file_path' => $newImage,
    //                 'user_id' => $user->id,

    //             ]);
    //         }
    //     }

    //     return $this->respondResource(
    //         new VisitIndexResource($new_visit->load(['files'])),
    //         ['message' => 'Visit created successfully']
    //     );
    // }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        return $this->respondResource(new SilverSilverIndexResource(SilverPrice::findOrFail($id)));
    }

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

        $silver = silverPrice::findOrFail($id);
        $data = $request->except('icon');

        if ($request->hasFile('icon')) {
            $image = (new FileUploader())->save($request->icon, 'silvers');
            $silver->silver->update([
                'icon' => $image
            ]);
        }
        $silver->update($data);
        return $this->respondResource(new SilverSilverIndexResource($silver->fresh()));
    }

    /**
     * Remove the specified resource from storage.
     */
}
