<?php

namespace App\Http\Controllers;

use App\Models\StageAcademyTrainer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class StageAcademyTrainersController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:stage_academy.trainers.view')->only(['index']);
        $this->middleware('permission:stage_academy.trainers.create')->only(['store']);
        $this->middleware('permission:stage_academy.trainers.update')->only(['update']);
        $this->middleware('permission:stage_academy.trainers.delete')->only(['destroy']);
    }

    public function index()
    {
        $trainers = StageAcademyTrainer::query()
            ->orderByDesc('id')
            ->get();

        return view('stage_academy.trainers.index', compact('trainers'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name_ar' => ['required', 'string', 'max:255'],
            'name_en' => ['required', 'string', 'max:255'],
            'details_ar' => ['nullable', 'string'],
            'details_en' => ['nullable', 'string'],
            'image' => ['nullable', 'image'],
            'percentage' => ['required', 'numeric', 'min:0', 'max:100'],
            'facebook' => ['nullable', 'url'],
            'instagram' => ['nullable', 'url'],
            'tiktok' => ['nullable', 'url'],
            'youtube' => ['nullable', 'url'],
            'twitter' => ['nullable', 'url'],
            'website' => ['nullable', 'url'],
        ]);

        if ($request->hasFile('image')) {
            $validated['image'] = $request->file('image')->store('stage-academy', 'public');
        }

        StageAcademyTrainer::create($validated);

        return redirect()->route('stage-academy.trainers');
    }

    public function update(Request $request, int $id)
    {
        $trainer = StageAcademyTrainer::query()->findOrFail($id);

        $validated = $request->validate([
            'name_ar' => ['required', 'string', 'max:255'],
            'name_en' => ['required', 'string', 'max:255'],
            'details_ar' => ['nullable', 'string'],
            'details_en' => ['nullable', 'string'],
            'image' => ['nullable', 'image'],
            'percentage' => ['required', 'numeric', 'min:0', 'max:100'],
            'facebook' => ['nullable', 'url'],
            'instagram' => ['nullable', 'url'],
            'tiktok' => ['nullable', 'url'],
            'youtube' => ['nullable', 'url'],
            'twitter' => ['nullable', 'url'],
            'website' => ['nullable', 'url'],
        ]);

        if ($request->hasFile('image')) {
            if ($trainer->image) {
                Storage::disk('public')->delete($trainer->image);
            }

            $validated['image'] = $request->file('image')->store('stage-academy', 'public');
        }

        $trainer->update($validated);

        return redirect()->route('stage-academy.trainers');
    }

    public function destroy(int $id)
    {
        $trainer = StageAcademyTrainer::query()->findOrFail($id);

        $trainer->delete();

        return redirect()->route('stage-academy.trainers');
    }
}
