<?php

namespace App\Http\Controllers;

use App\Models\NewsletterSubscriber;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class NewsletterController extends Controller
{
    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'email' => ['required', 'email:rfc', 'max:255'],
        ]);

        NewsletterSubscriber::query()->updateOrCreate(
            ['email' => strtolower($data['email'])],
            ['is_active' => true, 'subscribed_at' => now()]
        );

        return back()->with('newsletter_status', 'You are subscribed to 8Stores updates.');
    }
}
