<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class SettingsController extends Controller
{
    // ─────────────────────────────────────────
    // PROFILE
    // ─────────────────────────────────────────
    public function profile()
    {
        return view('settings.profile', ['user' => Auth::user()]);
    }

    public function updateProfile(Request $request)
    {
        $user = Auth::user();

        $data = $request->validate([
            'name'          => ['required', 'string', 'max:100'],
            'business_name' => ['required', 'string', 'max:150'],
            'business_type' => ['nullable', 'string', 'max:50'],
            'phone'         => ['nullable', 'string', 'max:20'],
            'address'       => ['nullable', 'string', 'max:500'],
            'logo'          => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:2048'],
            'password'      => ['nullable', 'confirmed', 'min:8'],
        ]);

        if ($request->hasFile('logo')) {
            $data['logo_path'] = $request->file('logo')->store('logos', 'public');
        }

        if (! empty($data['password'])) {
            $data['password'] = Hash::make($data['password']);
        } else {
            unset($data['password']);
        }

        unset($data['logo']);
        $user->update($data);

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

    // ─────────────────────────────────────────
    // INVOICE SETTINGS
    // ─────────────────────────────────────────
    public function invoice()
    {
        return view('settings.invoice', ['user' => Auth::user()]);
    }

    public function updateInvoice(Request $request)
    {
        $data = $request->validate([
            'invoice_prefix'        => ['required', 'string', 'max:10', 'alpha_num'],
            'payment_terms'         => ['required', 'integer', 'min:1', 'max:365'],
            'default_invoice_notes' => ['nullable', 'string', 'max:1000'],
        ]);

        Auth::user()->update($data);

        return back()->with('success', 'Invoice settings updated.');
    }

    // ─────────────────────────────────────────
    // BANK DETAILS
    // ─────────────────────────────────────────
    public function bank()
    {
        return view('settings.bank', ['user' => Auth::user()]);
    }

    public function updateBank(Request $request)
    {
        $data = $request->validate([
            'bank_name'      => ['required', 'string', 'max:100'],
            'account_number' => ['required', 'string', 'max:20'],
            'account_name'   => ['required', 'string', 'max:150'],
        ]);

        $data['account_name'] = strtoupper($data['account_name']);
        Auth::user()->update($data);

        return back()->with('success', 'Bank details updated.');
    }

    // ─────────────────────────────────────────
    // NOTIFICATIONS
    // ─────────────────────────────────────────
    public function notifications()
    {
        return view('settings.notifications', ['user' => Auth::user()]);
    }

    public function updateNotifications(Request $request)
    {
        Auth::user()->update([
            'notify_invoice_sent'      => $request->boolean('notify_invoice_sent'),
            'notify_payment_received'  => $request->boolean('notify_payment_received'),
            'notify_payment_reminder'  => $request->boolean('notify_payment_reminder'),
            'notify_overdue'           => $request->boolean('notify_overdue'),
        ]);

        return back()->with('success', 'Notification preferences saved.');
    }

    // ─────────────────────────────────────────
    // SUBSCRIPTION
    // ─────────────────────────────────────────
    public function subscription()
    {
        $user  = Auth::user();
        $plans = config('invotrack.plans');

        return view('settings.subscription', compact('user', 'plans'));
    }
}
