<?php

namespace App\Http\Controllers;

use App\Models\ContactMessage;
use App\Models\NewsletterSubscription;
use App\Models\GalleryItem;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function index()
    {
        return view('contact');
    }

    public function storeMessage(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'nullable|string|max:20',
            'subject' => 'required|string|max:255',
            'message' => 'required|string',
        ]);

        // Dynamic creation: since ContactMessage model isn't strictly needed for saving (we can insert directly via DB,
        // or create the ContactMessage model. Let's create it in database query directly, or write ContactMessage model!)
        // Let's use direct database insert or write the ContactMessage Model to follow standards!
        // First, let's do a direct insert via query builder to keep things fast, or write the model.
        // Actually, let's write the model to be clean! We will create the ContactMessage Model right after this.
        \App\Models\ContactMessage::create($request->only('name', 'email', 'phone', 'subject', 'message'));

        return back()->with('success', 'Thank you! Your message has been sent. We will get back to you shortly.');
    }

    public function subscribeNewsletter(Request $request)
    {
        $request->validate([
            'email' => 'required|email|unique:newsletter_subscriptions,email',
        ]);

        NewsletterSubscription::create([
            'email' => $request->email,
            'is_active' => true,
        ]);

        return back()->with('success', 'Awesome! You have successfully subscribed to MJ\'s newsletter.');
    }

    public function showGallery()
    {
        $galleryItems = GalleryItem::latest()->get();
        return view('gallery', compact('galleryItems'));
    }
}
