<?php

namespace App\Http\Controllers\Admin\Contact;

use App\Http\Controllers\Controller;
use App\Models\Contact;
use App\Models\NotificationTemplate;
use App\Notifications\UserManualMailNotification;
use App\Services\ModalIndexQuey;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        adminUserHasPermission(permission: 'read');

        $contacts = ModalIndexQuey::get(Contact::query());

        $columns = [
            [
                'label' => __('Name'),
                'key' => 'name',
                'header_class' => 'lg:w-[350px]',
                'is_sortable' => true,
            ],
            [
                'label' => __('Content'),
                'key' => 'email',
                'header_class' => 'lg:w-[200px]',
            ],
            [
                'label' => __('Action'),
                'render' => function ($contact) {
                    $action_buttons = [
                        [
                            'label' => __('Send Email'),
                            'icon' => 'ph ph-eye',
                            'type' => 'link',
                            'href' => route('admin.contacts.show', $contact->id),
                        ],
                        [
                            'label' => __('Delete'),
                            'icon' => 'ph ph-trash',
                            'type' => 'delete',
                            'href' => route('admin.contacts.destroy', $contact->id),
                        ],
                    ];

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

        return view('admin.pages.contact.index', compact('contacts', 'columns'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function show(Contact $contact)
    {
        adminUserHasPermission(permission: 'read');

        $buttons = [
            [
                'title' => __('Back'),
                'icon' => 'ph ph-arrow-left',
                'type' => 'link',
                'link' => route('admin.contacts.index'),
            ],
        ];

        $templates = NotificationTemplate::notDefault()->get();

        return view('admin.pages.contact.show', compact('contact', 'buttons', 'templates'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function update(Request $request, Contact $contact)
    {
        adminUserHasPermission(permission: 'edit');
        $request->validate([
            'email_template_id' => 'nullable|exists:notification_templates,id',
            'subject' => 'nullable|string|max:255',
            'body' => 'nullable|string',
        ]);

        if (! $request->email_template_id && ! $request->subject && ! $request->message) {
            return back()->with('error', __('Please enter subject and message or select email template'));
        }

        $subject = $request->subject;

        $message = inputSanitize($request->body);

        if ($request->email_template_id) {
            $template = NotificationTemplate::find($request->email_template_id);

            $subject = $template->subject;

            $message = $template->body;
        }

        if (! $subject || ! $message) {
            return back()->with('error', __('Please enter subject and message'));
        }

        $contact->notify(new UserManualMailNotification(
            $contact,
            $subject,
            $message
        ));

        return response()->json([
            'message' => __('Email sent successfully'),
        ]);
    }

    public function deleteAll(Request $request)
    {
        adminUserHasPermission(permission: 'delete');
        $ids = $request->ids;

        if (isset($ids)) {
            Contact::destroy($ids);

            return back()->with('success', __('Contacts deleted successfully'));
        }

        Contact::query()->delete();

        return back()->with('success', __('Contacts deleted successfully'));
    }
}
