<?php

declare(strict_types=1);

namespace App\Http\Controllers\Customer;

use App\Domain\Account\Models\Account;
use App\Domain\Domain\Models\Domain;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Inertia\Response;

class MailingListController extends Controller
{
    public function index(Request $request): Response
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $lists = DB::table('mailing_lists')
            ->where('account_id', $account->id)
            ->whereNull('deleted_at')
            ->latest()
            ->get();

        $domains = Domain::where('account_id', $account->id)
            ->whereIn('type', ['main', 'addon'])
            ->whereNull('deleted_at')
            ->pluck('name')
            ->all();

        return Inertia::render('Customer/Email/MailingLists', [
            'lists' => $lists,
            'available_domains' => $domains,
        ]);
    }

    public function store(Request $request): RedirectResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $validated = $request->validate([
            'local' => ['required', 'string', 'regex:/^[a-z0-9._-]+$/i', 'max:64'],
            'domain' => ['required', 'string', 'max:253'],
            'description' => ['nullable', 'string', 'max:191'],
            'access_type' => ['required', 'in:public,private,announce_only,moderated'],
            'admin_password' => ['required', 'string', 'min:8'],
        ]);

        if (DB::table('mailing_lists')
            ->where('local', $validated['local'])
            ->where('domain', $validated['domain'])
            ->whereNull('deleted_at')
            ->exists()) {
            return back()->with('error', 'Bu liste zaten var.');
        }

        DB::table('mailing_lists')->insert([
            'account_id' => $account->id,
            'local' => $validated['local'],
            'domain' => $validated['domain'],
            'description' => $validated['description'] ?? null,
            'access_type' => $validated['access_type'],
            'admin_password_hash' => password_hash($validated['admin_password'], PASSWORD_BCRYPT),
            'is_active' => true,
            'subscribers_count' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return back()->with('success', "Mailing list '{$validated['local']}@{$validated['domain']}' oluşturuldu.");
    }

    public function destroy(Request $request, int $id): RedirectResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        DB::table('mailing_lists')
            ->where('id', $id)
            ->where('account_id', $account->id)
            ->update(['deleted_at' => now()]);

        return back()->with('success', 'Mailing list silindi.');
    }
}
