<?php

namespace App\Http\Controllers;

use App\Models\HomeValuation;
use Illuminate\Http\Request;

class ValuationController extends Controller
{
    public function index()
    {
        return view('pages.valuation');
    }

    public function store(Request $request)
    {
        $request->validate([
            'owner_name' => 'required|string|max:255',
            'owner_email' => 'required|email',
            'owner_phone' => 'required|string',
            'property_address' => 'required|string',
            'city' => 'required|string',
            'property_type' => 'required|string',
            'bedrooms' => 'required|integer',
        ]);

        // Estimated Valuation Calculation Logic based on city & bedrooms
        $baseRate = strtolower($request->city) === 'abuja' ? 85000000 : 75000000;
        $estimatedValue = $baseRate + ($request->bedrooms * 15000000);

        $valuation = HomeValuation::create([
            'owner_name' => $request->owner_name,
            'owner_email' => $request->owner_email,
            'owner_phone' => $request->owner_phone,
            'property_address' => $request->property_address,
            'city' => $request->city,
            'property_type' => $request->property_type,
            'bedrooms' => $request->bedrooms,
            'condition' => $request->condition ?? 'Good',
            'estimated_value' => $estimatedValue,
            'status' => 'Automated Estimate Generated',
        ]);

        return back()->with('valuation_result', [
            'value' => number_format($estimatedValue, 0),
            'address' => $request->property_address,
            'city' => $request->city,
        ])->with('success', 'Your instant automated property valuation estimate has been generated!');
    }
}
