<?php

namespace App\Http\Controllers\Auth;

use App\Helpers\AutoNumbering;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\ImageManager;

class ProfileController extends Controller
{
    public function index(Request $request)
    {
        if (! auth()->check()) {
            return redirect()->route('login');
        }

        return view('account.info');
    }

    public function update(Request $request)
    {
        try {

            $request->validate([
                'name' => 'required|string|max:255',
                'birth_date' => 'nullable|date',
                'email' => 'required|email|max:255',
                'phone' => 'required|string|max:255',
                'photo' => 'required|image|mimes:jpg,jpeg,png,webp|max:2048',
            ]);

            $user = auth()->user();
            $user->name = $request->name;
            $user->email = $request->email;
            $user->phone = $request->phone;
            
            // Handle avatar upload
            if ($request->hasFile('photo')) {
                $ext = $request->file('photo')->extension();
                $filename = $request->file('photo')->storeAs("profile", $user->id.".".$ext, "public");
                $user->photo = $filename;
            }
            
            $user->save();

            $customer = $user->customer;
            if ($user->customer == null) {
                $customer = new Customer;

                $autoNumbering = new AutoNumbering([
                    'type' => 'CUST',
                    'prefix' => 'CAPP',
                    'location_id' => 0,
                    'pad' => 9,
                ]);
                do {
                    $number = $autoNumbering->getCurrent();
                    $count = Customer::where('number', $number)->count();
                } while ($count > 0);

                $customer->number = $number;
                $customer->user_id = $user->id;

            }

            if ($request->name){
                $customer->name = $request->name;
            }

            if ($request->email) {
                $customer->email = $request->email;
            }

            if ($request->phone) {
                $customer->phone = $request->phone;
            }

            if ($request->birth_date != null) {
                $customer->date_of_birth = $request->birth_date;
            }

            $customer->save();

            return back()->with('success', 'Profile updated successfully!');
        } catch (Exception $e) {
            Log::error($e);
            return back()->with('error', $e->getMessage());
        }
    }


    public function updatePassword(Request $request)
    {
        try {

            $request->validate([
                'current_password' => 'required|string',
                'password' => 'required|string|min:8|confirmed',
            ]);

            $user = auth()->user();
            
            // Verify current password
            if (!Hash::check($request->current_password, $user->password)) {
                return back()->with('error', 'Current password is incorrect.');
            }

            $user->password = bcrypt($request->password);
            $user->save();

            return back()->with('success', 'Password updated successfully!');
        } catch (Exception $e) {
            return back()->with('error', $e->getMessage());
        }
    }
}
