<?php

declare(strict_types=1);

namespace App\Http\Controllers\Account;

use App\Http\Controllers\Controller;
use App\Http\Requests\UpdatePasswordRequest;
use App\Http\Requests\UpdateProfileRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class ProfileController extends Controller
{
    public function show(Request $request): Response
    {
        $user = $request->user();
        $user->load('profile');

        $profile = $user->profile;

        $loginHistories = $user->loginHistories()
            ->latest()
            ->limit(10)
            ->get();

        $trustedDevices = $user->trustedDevices()
            ->active()
            ->latest('last_used_at')
            ->get();

        return Inertia::render('Profile/Show', [
            'user' => $user,
            'profile' => $profile ? [
                'billing_address_line1' => $profile->billing_address_line1,
                'billing_address_line2' => $profile->billing_address_line2,
                'billing_city' => $profile->billing_city,
                'billing_state' => $profile->billing_state,
                'billing_zip' => $profile->billing_zip,
                'billing_country' => $profile->billing_country,
                'tax_id' => $profile->tax_id,
                'tax_exempt' => $profile->tax_exempt,
                'company_name' => $profile->company_name,
                'company_vat' => $profile->company_vat,
            ] : null,
            'twoFactorEnabled' => (bool) $user->two_factor_secret,
            'loginHistories' => $loginHistories,
            'trustedDevices' => $trustedDevices,
        ]);
    }

    public function update(UpdateProfileRequest $request): RedirectResponse
    {
        $validated = $request->validated();
        $user = $request->user();

        $user->update([
            'name' => $validated['first_name'].' '.$validated['last_name'],
            'phone' => $validated['phone'] ?? null,
            'company' => $validated['company'] ?? null,
        ]);

        $user->profile()->updateOrCreate(
            ['user_id' => $user->id],
            [
                'billing_address_line1' => $validated['address_line1'] ?? null,
                'billing_address_line2' => $validated['address_line2'] ?? null,
                'billing_city' => $validated['city'] ?? null,
                'billing_state' => $validated['state'] ?? null,
                'billing_zip' => $validated['zip'] ?? null,
                'billing_country' => $validated['country'] ?? null,
            ]
        );

        return back()->with('success', 'Profile updated successfully.');
    }

    public function updatePassword(UpdatePasswordRequest $request): RedirectResponse
    {
        $request->user()->update([
            'password' => $request->validated('password'),
        ]);

        return back()->with('success', 'Password updated successfully.');
    }

    public function updateBilling(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'billing_address_line1' => ['nullable', 'string', 'max:255'],
            'billing_address_line2' => ['nullable', 'string', 'max:255'],
            'billing_city' => ['nullable', 'string', 'max:100'],
            'billing_state' => ['nullable', 'string', 'max:100'],
            'billing_zip' => ['nullable', 'string', 'max:20'],
            'billing_country' => ['nullable', 'string', 'max:100'],
            'tax_id' => ['nullable', 'string', 'max:50'],
            'company_vat' => ['nullable', 'string', 'max:50'],
        ]);

        $request->user()->profile()->updateOrCreate(
            ['user_id' => $request->user()->id],
            $validated
        );

        return back()->with('success', 'Billing information updated successfully.');
    }
}
