<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\ServiceRequest;
use App\Models\Service;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;

class ServiceController extends Controller
{
    public function index(): View
    {
        return view('pages.admin.master.service.index', [
            'pageTitle' => 'Layanan',
            'services' => Service::with(['user', 'user.biography'])
                ->barbershop()
                ->latest()
                ->get(),
        ]);
    }

    public function create(): View
    {
        return view('pages.admin.master.service.create', [
            'pageTitle' => 'Tambah Layanan',
        ]);
    }

    public function store(ServiceRequest $request): RedirectResponse
    {
        $validatedData = $request->validated();
        Service::create(array_merge($validatedData, [
            'user_id' => Auth::id(),
            'barbershop_id' => Auth::user()->barbershop_id,
        ]));

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

        return redirect('dashboard/master/services');
    }

    public function edit(Service $service): View
    {
        return view('pages.admin.master.service.edit', [
            'pageTitle' => 'Edit Layanan',
            'service' => $service,
        ]);
    }

    public function update(Service $service, ServiceRequest $request): RedirectResponse
    {
        $validatedData = $request->validated();

        $service->update($validatedData);

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

        return redirect('dashboard/master/services');
    }

    public function delete(Service $service): RedirectResponse
    {
        $service->delete();

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

        return back();
    }
}
