<?php

namespace App\Http\Controllers\Ticket;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Ticket\StoreTicketRequest;
use App\Http\Requests\Ticket\UpdateTicketRequest;
use App\Http\Resources\Ticket\TicketResource;
use App\Models\Ticket;
use App\Services\Ticket\TicketService;
use Illuminate\Http\Request;

class TicketController extends ApiController
{
    public function __construct(
        protected TicketService $service
    ) {}

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

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

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

        return $this->created(
            new TicketResource($ticket),
            'Ticket berhasil dibuat'
        );
    }

    public function show(Ticket $ticket)
    {
        return $this->success(
            new TicketResource($ticket)
        );
    }

    public function update(
        UpdateTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->update(
            $ticket,
            $request->validated()
        );

        return $this->updated(
            new TicketResource($ticket),
            'Ticket berhasil diperbarui'
        );
    }

    public function destroy(Ticket $ticket)
    {
        $this->service->delete($ticket);

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