<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class FrontController extends Controller
{
    public function home()
    {
        return view('front.home', [
            'services' => DB::table('services')->where('status', 'active')->limit(8)->get(),
            'banners' => DB::table('app_banners')->where('status', 'active')->orderBy('sort_order')->get(),
        ]);
    }

    public function customerRegister()
    {
        return view('front.customer-register');
    }

    public function storeCustomer(Request $request)
    {
        $data = $request->validate([
            'name' => ['required', 'string', 'max:120'],
            'phone' => ['required', 'string', 'max:32'],
            'email' => ['nullable', 'email', 'max:150'],
            'address_line_1' => ['required', 'string', 'max:255'],
            'pincode' => ['nullable', 'string', 'max:12'],
        ]);

        $user = User::firstOrCreate(
            ['phone' => $data['phone']],
            [
                'uuid' => (string) Str::uuid(),
                'name' => $data['name'],
                'email' => $data['email'] ?? null,
                'phone_country_code' => '+91',
                'role' => 'customer',
                'status' => 'active',
                'password' => Hash::make('customer123'),
            ]
        );

        DB::table('customer_addresses')->insert([
            'user_id' => $user->id,
            'label' => 'Home',
            'address_line_1' => $data['address_line_1'],
            'pincode' => $data['pincode'] ?? null,
            'is_default' => true,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return redirect()->route('front.customer.register')->with('status', 'Customer registration submitted.');
    }

    public function vendorRegister()
    {
        return view('front.vendor-register');
    }

    public function storeVendor(Request $request)
    {
        $data = $request->validate([
            'name' => ['required', 'string', 'max:120'],
            'phone' => ['required', 'string', 'max:32'],
            'email' => ['nullable', 'email', 'max:150'],
            'vendor_business_name' => ['nullable', 'string', 'max:160'],
            'full_address' => ['required', 'string', 'max:500'],
            'bank_name' => ['nullable', 'string', 'max:120'],
            'bank_account_number' => ['nullable', 'string', 'max:80'],
            'bank_ifsc' => ['nullable', 'string', 'max:40'],
        ]);

        $user = User::firstOrCreate(
            ['phone' => $data['phone']],
            [
                'uuid' => (string) Str::uuid(),
                'name' => $data['name'],
                'email' => $data['email'] ?? null,
                'phone_country_code' => '+91',
                'role' => 'professional',
                'status' => 'active',
                'password' => Hash::make('expert123'),
            ]
        );

        DB::table('professionals')->updateOrInsert(
            ['user_id' => $user->id],
            [
                'pro_code' => 'RS-PRO-' . strtoupper(Str::random(5)),
                'vendor_business_name' => $data['vendor_business_name'] ?? $data['name'],
                'onboarding_status' => 'lead',
                'employment_type' => 'partner',
                'status' => 'lead',
                'online_status' => 'offline',
                'bank_name' => $data['bank_name'] ?? null,
                'bank_account_number' => $data['bank_account_number'] ?? null,
                'bank_ifsc' => $data['bank_ifsc'] ?? null,
                'full_address' => $data['full_address'],
                'document_status' => 'pending',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        );

        return redirect()->route('front.vendor.register')->with('status', 'Vendor registration submitted.');
    }
}
