<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\CustomerRequest;
use App\Models\Customer;
use App\Traits\UploadAttachment;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;

class CustomerController extends Controller
{
    use UploadAttachment;

    public function index(): View
    {
        return view('pages.admin.master.customers', [
            'pageTitle' => 'Pelanggan',
            'customers' => Customer::with(['user', 'user.biography'])
                ->barbershop()
                ->latest()
                ->get(),
        ]);
    }

    public function store(CustomerRequest $request): RedirectResponse
    {
        $validatedData = $request->validated();

        Customer::create(array_merge($validatedData, [
            'user_id' => Auth::id(),
            'barbershop_id' => Auth::user()->barbershop_id,
        ]));

        notify()->success('Data berhasil ditambahkan', 'Berhasil');

        return back();
    }

    public function update(Customer $customer, CustomerRequest $request): RedirectResponse
    {
        $validatedData = $request->validated();

        $customer->update($validatedData);

        notify()->success('Data berhasil diubah', 'Berhasil');

        return back();
    }

    public function delete(Customer $customer): RedirectResponse
    {
        $customer->delete();

        notify()->success('Data berhasil dihapus', 'Berhasil');

        return back();
    }
}
