<?php

namespace App\Http\Controllers\Admin;

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

class NewsletterController extends Controller
{
    public function index()
    {
        $subscribers = Newsletter::latest()->paginate(15);
        return view('admin.newsletter.index', compact('subscribers'));
    }

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

        Newsletter::create([
            'email' => $request->email,
            'status' => 'active'
        ]);

        return back()->with('success', 'Thank you for subscribing to our newsletter!');
    }

    public function sendNewsletter(Request $request)
    {
        $request->validate([
            'subject' => 'required|string|max:255',
            'content' => 'required|string',
        ]);

        // Here you would implement the actual newsletter sending logic
        // For now, we'll just return a success message

        return redirect()->route('admin.newsletter.index')->with('success', 'Newsletter sent successfully!');
    }

    public function destroy(Newsletter $newsletter)
    {
        $newsletter->delete();

        return redirect()->route('admin.newsletter.index')->with('success', 'Subscriber removed successfully!');
    }
}
