<?php

namespace App\Http\Controllers;

use App\Models\ContactMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class ContactController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:view contact messages', ['only' => ['adminIndex', 'show']]);
        $this->middleware('permission:update contact messages', ['only' => ['edit', 'update']]);
        $this->middleware('permission:delete contact messages', ['only' => ['destroy']]);
    }

    /*
    |--------------------------------------------------------------------------
    | Frontend Contact Page
    |--------------------------------------------------------------------------
    */

    public function index()
    {
        return view('user.contact');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name'    => 'required|string|max:150',
            'email'   => 'required|email|max:150',
            'subject' => 'required|string|max:200',
            'message' => 'required|string|max:3000',
        ]);

        ContactMessage::create([
            'name'    => $validated['name'],
            'email'   => $validated['email'],
            'subject' => $validated['subject'],
            'message' => $validated['message'],
            'status'  => 'new',
        ]);

        return redirect()
            ->back()
            ->with('success', 'Your message has been saved successfully. Our admin team will contact you soon.');
    }

    /*
    |--------------------------------------------------------------------------
    | Contact Messages List
    |--------------------------------------------------------------------------
    */

    public function adminIndex()
    {
        $contactMessages = ContactMessage::latest()->get();

        return view('contact-messages.index', compact('contactMessages'));
    }

    public function show(ContactMessage $contactMessage)
    {
        return response()->json([
            'status'  => true,
            'contact' => $this->formatContactMessage($contactMessage),
        ]);
    }

    public function edit(ContactMessage $contactMessage)
    {
        return response()->json([
            'status'  => true,
            'contact' => $this->formatContactMessage($contactMessage),
        ]);
    }

    public function update(Request $request, ContactMessage $contactMessage)
    {
        $validated = $request->validate([
            'name'    => 'required|string|max:150',
            'email'   => 'required|email|max:150',
            'subject' => 'required|string|max:200',
            'message' => 'required|string|max:3000',
            'status'  => 'required|string|in:new,contacted,closed',
        ]);

        $contactMessage->update([
            'name'    => $validated['name'],
            'email'   => $validated['email'],
            'subject' => $validated['subject'],
            'message' => $validated['message'],
            'status'  => $validated['status'],
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Contact message updated successfully.',
            'contact' => $this->formatContactMessage($contactMessage->fresh()),
        ]);
    }

    public function destroy(ContactMessage $contactMessage)
    {
        $contactMessage->delete();

        return response()->json([
            'status'  => true,
            'message' => 'Contact message deleted successfully.',
        ]);
    }

    private function formatContactMessage(ContactMessage $contactMessage): array
    {
        return [
            'id'             => $contactMessage->id,
            'name'           => $contactMessage->name,
            'email'          => $contactMessage->email,
            'subject'        => $contactMessage->subject,
            'message'        => $contactMessage->message,
            'status'         => $contactMessage->status,
            'status_label'   => ucfirst($contactMessage->status),
            'formatted_date' => $contactMessage->created_at
                ? Carbon::parse($contactMessage->created_at)->format('d M Y')
                : '-',
        ];
    }
}