<?php

namespace App\Http\Controllers;

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

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

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

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

    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');
        }

        StageAcademyInstitution::create($validated);

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

    public function update(Request $request, int $id)
    {
        $institution = StageAcademyInstitution::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 ($institution->image) {
                Storage::disk('public')->delete($institution->image);
            }

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

        $institution->update($validated);

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

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

        $institution->delete();

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