<?php

namespace Modules\SocialLink\app\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\File;
use Modules\SocialLink\app\Models\SocialLink;

class SocialLinkController extends Controller {
    /**
     * Display a listing of the resource.
     */
    public function index() {
        checkAdminHasPermissionAndThrowException('social.link.management');
        $socialLinks = SocialLink::paginate(25);
        return view('sociallink::index', compact('socialLinks'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create() {
        return view('sociallink::create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request) {

        checkAdminHasPermissionAndThrowException('social.link.management');
        $request->validate([
            'link' => ['required'],
            'icon' => ['required', 'image'],
        ],[
            'icon.required'=> 'The icon is required.',
            'link.required'=> 'Link is required',
        ]);

        $file_name = '';
        if ($request->hasFile('icon')) {
            $file_name = uploadAndOptimizeImage(
                file: $request->icon,
                resize: [24,24]
            );
        }

        SocialLink::create(
            [
                'link' => $request->link,
                'icon' => $file_name,
            ]
        );
        cache()->forget('getSocialLinks');

        return redirect()->route('admin.social-link.index')->with(['message' => __('Updated successfully'), 'alert-type' => 'success']);

    }

    /**
     * Show the specified resource.
     */
    public function show($id) {
        return view('sociallink::show');
    }

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

        checkAdminHasPermissionAndThrowException('social.link.management');
        $socialLink = SocialLink::findOrFail($id);
        return view('sociallink::edit', compact('socialLink'));
    }

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

        checkAdminHasPermissionAndThrowException('social.link.management');
        $request->validate([
            'link' => ['required'],
            'icon' => ['nullable', 'image'],
        ],[
            'icon.required'=> 'The icon is required.',
            'link.required'=> 'Link is required',
        ]);

        $socialLink = SocialLink::findOrFail($id);
        $data = [];
        $data['link'] = $request->link;

        if ($request->hasFile('icon')) {
            $data['icon'] = uploadAndOptimizeImage(
                file: $request->icon,
                oldFile: $socialLink?->icon,
                resize: [24,24]
            );
        }
        $socialLink->update($data);
        cache()->forget('getSocialLinks');

        return redirect()->route('admin.social-link.index')->with(['message' => __('Updated successfully'), 'alert-type' => 'success']);

    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id) {
        checkAdminHasPermissionAndThrowException('social.link.management');
        $socialLink = SocialLink::findOrFail($id);
        if ($socialLink->icon) {
            if (File::exists(public_path($socialLink->icon))) {
                unlink(public_path($socialLink->icon));
            }
        }
        $socialLink->delete();
        cache()->forget('getSocialLinks');
        return redirect()->route('admin.social-link.index')->with(['message' => __('Deleted Successfully'), 'alert-type' => 'success']);
    }
}
