<?php

namespace App\Http\Controllers\Admin\Customers;

use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Services\CustomerRiskService;
use Illuminate\Http\Request;

class CustomerRatingController extends Controller
{
    protected $riskService;

    public function __construct(CustomerRiskService $riskService)
    {
        $this->riskService = $riskService;
    }

    public function index()
    {
        $customers = Customer::with('rating', 'violations', 'paymentDelays', 'blacklistEntries')
            ->orderBy('created_at', 'desc')
            ->paginate(15);

        return view('admin.customers.ratings.index', compact('customers'));
    }

    public function show(Customer $customer)
    {
        $customer->load('rating', 'violations', 'paymentDelays', 'blacklistEntries', 'rentals');
        $riskStatus = $this->riskService->getRiskStatus($customer);

        return view('admin.customers.ratings.show', compact('customer', 'riskStatus'));
    }

    public function updateRiskNotes(Request $request, Customer $customer)
    {
        $validated = $request->validate([
            'notes' => 'required|string|max:500',
        ]);

        $rating = $customer->rating ?? $customer->createRating();
        $rating->update([
            'notes' => $validated['notes'],
            'updated_by' => auth()->id(),
        ]);

        return back()->with('success', app()->getLocale() == 'ar' ? 'تم تحديث الملاحظات بنجاح' : 'Notes updated successfully');
    }
}
