<?php

namespace App\Http\Controllers\Admin\Settings;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

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

        $buttons = [
            [
                'label' => __('Add New Social Media'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'modal',
                'id' => 'social_add_modal_id',
                'href' => route('admin.settings.social-medias.store'),
            ],
        ];

        $socials = [];

        foreach (config('application_info.social_medias') as $value) {
            $socials[] = [
                'id' => $value['id'],
                'name' => $value['name'],
                'link' => $value['link'],
                'icon' => $value['icon'] ?? null,
            ];
        }

        $columns = [
            [
                'label' => __('Name'),
                'key' => 'name',
            ],
            [
                'label' => __('Link'),
                'key' => 'link',
            ],

            [
                'label' => __(key: 'Icon'),
                'key' => 'icon',
                'render' => function ($social) {
                    return $social['icon'] ? '<i class="'.$social['icon'].'"></i>' : '';
                },
            ],

            [
                'label' => __('Action'),
                'render' => function ($social) {

                    $action_buttons = [
                        [
                            'label' => __('Edit'),
                            'icon' => 'ph ph-pencil',
                            'id' => 'social_edit_modal_id',
                            'row' => json_encode($social),
                            'type' => 'modal',
                            'href' => route('admin.settings.social-medias.update', $social['id']),

                        ],
                        [
                            'label' => __('Delete'),
                            'icon' => 'ph ph-trash',
                            'type' => 'delete',
                            'href' => route('admin.settings.social-medias.destroy', $social['id']),
                        ],
                    ];

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

        return view('admin.pages.settings.social-media.index', compact('socials', 'columns', 'buttons'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'link' => 'required|string|max:255',
            'icon' => 'required|string|max:255',
        ]);

        $old = config('application_info.social_medias');

        $new = [...$old, [...$validated, 'id' => count($old) + 1]];

        storeOption([
            'social_medias' => json_encode($new),
        ]);

        return response()->json([
            'message' => __('New social media has been added successfully'),
            'reload' => true,
        ]);
    }

    public function update(Request $request, $id)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'link' => 'required|string|max:255',
            'icon' => 'required|string|max:255',
        ]);

        $old = config('application_info.social_medias');

        $new = [];

        foreach ($old as $value) {
            if ($value['id'] == $id) {
                $new[] = [...$value, ...$validated];
            } else {
                $new[] = $value;
            }
        }

        storeOption([
            'social_medias' => json_encode($new),
        ]);

        return response()->json([
            'message' => __('New social media has been updated successfully'),
            'reload' => true,
        ]);
    }

    public function destroy($id)
    {
        $old = config('application_info.social_medias');

        $new = [];

        foreach ($old as $value) {
            if ($value['id'] != $id) {
                $new[] = $value;
            }
        }

        storeOption([
            'social_medias' => json_encode($new),
        ]);

        return response()->json([
            'message' => __('Social media has been deleted successfully'),
            'reload' => true,
        ]);
    }
}
