<?php

namespace App\Http\Controllers\Admin\Pages\Teams;

use App\Http\Controllers\Controller;
use App\Http\Requests\Teams\TeamStoreRequest;
use App\Http\Requests\Teams\TeamUpdateRequest;
use App\Models\Team;
use App\Traits\Description;
use App\Traits\Duty;
use App\Traits\Image;
use App\Traits\ImageCompressor;
use App\Traits\Title;
use DataTables;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class TeamController extends Controller
{
    use ImageCompressor,Description,Image,Title,Duty;

    public function __construct()
    {
        $this->middleware('permission:view teams')->only('index', 'ajax');
        $this->middleware('permission:create team')->only('create');
        $this->middleware('permission:store team')->only('store');
        $this->middleware('permission:edit team')->only('edit');
        $this->middleware('permission:update team')->only('update');
        $this->middleware('permission:destroy team')->only('destroy');
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('app.admin.pages.teams.all-team');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('app.admin.pages.teams.create-team');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(TeamStoreRequest $request)
    {
        $validatedData = $request->safe();
        dd($validatedData);

        try {
            if ($request->hasFile('image')) {
                $validatedData['image'] = self::compressImage($request->file('image'), 'TeamImage');
            }

            $team = Team::create($validatedData->except('partners'));


            return back()->with('success', __('Data successfully created!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(Team $team)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Team $team)
    {
        return view('app.admin.pages.teams.edit-team',compact([
            'team',
        ]));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(TeamUpdateRequest $request, Team $team)
    {
        $validatedData = $request->safe();
        try {
            if ($request->hasFile('image')) {
                File::delete('/storage/images/' . $team->getAttribute('image'));
                $validatedData['image'] = self::compressImage($request->file('image'), 'TeamImage');
            } else {
                $validatedData['image'] = $team->getAttribute('image');
            }

            $team->update($validatedData->except('partners'));


            return back()->with('success', __('Data successfully updated!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            Team::destroy($id);
            return back()->with('success', __('Data successfully deleted!'));
        } catch (Exception $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    /**
     * @return JsonResponse
     * @throws Exception
     */
    public function ajax()
    {
        $teams = Team::query();
        return DataTables::of($teams)->editColumn('image', function (Team $team) {
            return view('app.admin.includes.table-elements.image', [
                'image' => $team->image()
            ]);
        })->addColumn('options', function (Team $team) {
            return view('app.admin.includes.table-elements.action-buttons', [
                'modelName' => 'team',
                'editUrl' => route('admin:teams.edit', $team->getAttribute('id')),
                'deleteUrl' => route('admin:teams.destroy', $team->getAttribute('id'))
            ]);
        })->toJson();
    }
}
