<?php

namespace App\Http\Controllers\Ticket;

use App\Http\Controllers\ApiController;
use App\Http\Requests\TicketType\StoreTicketTypeRequest;
use App\Http\Requests\TicketType\UpdateTicketTypeRequest;
use App\Http\Resources\TicketType\TicketTypeResource;
use App\Models\TicketType;
use App\Services\Ticket\TicketTypeService;
use Illuminate\Http\Request;

class TicketTypeController extends ApiController
{
    public function __construct(
        protected TicketTypeService $service
    ) {}

    public function index(Request $request)
    {
        $data = TicketType::latest()->paginate(
            $request->get('per_page', 10)
        );

        return $this->success($data);
    }

    public function store(StoreTicketTypeRequest $request)
    {
        $type = $this->service->create(
            $request->validated()
        );

        return $this->created(
            new TicketTypeResource($type),
            'Ticket Type berhasil dibuat'
        );
    }

    public function show(TicketType $ticket_type)
    {
        return $this->success(
            new TicketTypeResource($ticket_type)
        );
    }

    public function update(
        UpdateTicketTypeRequest $request,
        TicketType $ticket_type
    ) {
        $type = $this->service->update(
            $ticket_type,
            $request->validated()
        );

        return $this->updated(
            new TicketTypeResource($type),
            'Ticket Type berhasil diupdate'
        );
    }

    public function destroy(TicketType $ticket_type)
    {
        $this->service->delete($ticket_type);

        return $this->deleted(
            'Ticket Type berhasil dihapus'
        );
    }
}
