<?php

namespace App\Http\Controllers\Admin\Settings;

use App\Http\Controllers\Controller;
use App\Models\Team;
use App\Services\ModalIndexQuey;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class TeamController extends Controller
{
    public function index()
    {
        adminUserHasPermission(permission: 'read');

        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'ph ph-arrow-clockwise',
                'type' => 'link',
                'link' => route('admin.settings.frontend-sections.edit', ['frontend_section' => 'teams']),
            ],
            [
                'label' => __('Create'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'link',
                'link' => route('admin.settings.teams.create'),
            ],
        ];

        $teams = ModalIndexQuey::get(model: Team::query());

        $columns = [
            [
                'label' => __('Name'),
                'key' => 'name',
                'header_class' => 'lg:w-[350px]',
            ],
            [
                'label' => __('Image'),
                'key' => 'image',
                'render' => function ($tame) {
                    return '<img src="'.placeImage($tame->image).'" class="w-12 h-12 rounded-full object-cover" alt="">';
                },
            ],
            [
                'label' => __('Designation'),
                'key' => 'designation',
            ],
            [
                'label' => __('Action'),
                'render' => function ($team) {
                    $action_buttons = [
                        [
                            'label' => __('Edit'),
                            'icon' => 'ph ph-pencil',
                            'type' => 'link',
                            'href' => route('admin.settings.teams.edit', $team->id),
                        ],
                        [
                            'label' => __('Delete'),
                            'icon' => 'ph ph-trash',
                            'type' => 'delete',
                            'class' => 'form-submit-delete',
                            'href' => route('admin.settings.teams.destroy', $team->id),
                        ],
                    ];

                    return view('admin.components.table-action', compact('action_buttons'))->render();
                },
            ],
        ];

        return view('admin.pages.settings.manage-pages-sections.teams.index', compact('teams', 'columns', 'buttons'));
    }

    public function create()
    {
        adminUserHasPermission(permission: 'create');

        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'ph ph-arrow-clockwise',
                'type' => 'link',
                'link' => route('admin.settings.teams.index', ['frontend_section' => 'teams']),
            ],
            [
                'label' => __('Back To Section List'),
                'icon' => 'ph ph-arrow-clockwise',
                'type' => 'link',
                'link' => route('admin.settings.frontend-sections.edit', ['frontend_section' => 'teams']),
            ],
        ];

        $socials = [
            [
                'label' => __('Facebook'),
                'key' => 'facebook',
                'icon' => 'ph ph-facebook-logo',
            ],
            [
                'label' => __('X'),
                'key' => 'x',
                'icon' => 'ph ph-x-logo',
            ],
            [
                'label' => __('Instagram'),
                'key' => 'instagram',
                'icon' => 'ph ph-instagram-logo',
            ],
            [
                'label' => __('Youtube'),
                'key' => 'youtube',
                'icon' => 'ph ph-youtube-logo',
            ],
        ];

        return view('admin.pages.settings.manage-pages-sections.teams.create', compact('socials', 'buttons'));
    }

    public function store(Request $request)
    {
        adminUserHasPermission(permission: 'create');

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'image' => 'required',
            'designation' => 'nullable|string|max:255',
            'socials' => 'nullable|array',
        ]);

        Team::create($validated);

        Cache::forget('teams_list');

        return response()->json(['reload' => true, 'message' => __('Team created successfully')]);
    }

    public function edit(Team $team)
    {
        adminUserHasPermission(permission: 'edit');

        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'ph ph-arrow-clockwise',
                'type' => 'link',
                'link' => route('admin.settings.teams.index', ['frontend_section' => 'teams']),
            ],
            [
                'label' => __('Back To Section List'),
                'icon' => 'ph ph-arrow-clockwise',
                'type' => 'link',
                'link' => route('admin.settings.frontend-sections.edit', ['frontend_section' => 'teams']),
            ],
        ];
        $socials = [
            [
                'label' => __('Facebook'),
                'key' => 'facebook',
                'icon' => 'ph ph-facebook-logo',
            ],
            [
                'label' => __('X'),
                'key' => 'x',
                'icon' => 'ph ph-x-logo',
            ],
            [
                'label' => __('Instagram'),
                'key' => 'instagram',
                'icon' => 'ph ph-instagram-logo',
            ],
            [
                'label' => __('Youtube'),
                'key' => 'youtube',
                'icon' => 'ph ph-youtube-logo',
            ],
        ];

        return view('admin.pages.settings.manage-pages-sections.teams.edit', compact('socials', 'buttons', 'team'));
    }

    public function update(Request $request, Team $team)
    {
        adminUserHasPermission(permission: 'edit');

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'image' => 'required|string',
            'designation' => 'nullable|string|max:255',
            'socials' => 'nullable|array',
        ]);

        $team->update($validated);

        Cache::forget('teams_list');

        return response()->json(['reload' => true, 'message' => __('Team updated successfully')]);
    }

    public function destroy(Team $team)
    {
        adminUserHasPermission(permission: 'delete');

        $team->delete();

        Cache::forget('teams_list');

        return response()->json(['success' => true, 'message' => __('Team deleted successfully')]);
    }
}
