<?php

namespace App\Http\Controllers\Ticket;

use App\Http\Controllers\ApiController;
use App\Http\Resources\Ticket\TicketResource;
use App\Models\Ticket;
use App\Services\Ticket\TicketWorkflowService;

use App\Http\Requests\Ticket\AssignTicketRequest;
use App\Http\Requests\Ticket\ReassignTicketRequest;
use App\Http\Requests\Ticket\ApproveTicketRequest;
use App\Http\Requests\Ticket\RejectTicketRequest;
use App\Http\Requests\Ticket\PendingTicketRequest;
use App\Http\Requests\Ticket\ResolveTicketRequest;
use App\Http\Requests\Ticket\CloseTicketRequest;
use App\Http\Requests\Ticket\CancelTicketRequest;
use App\Http\Requests\Ticket\EscalateTicketRequest;
use App\Http\Requests\Ticket\StartTicketRequest;

class TicketWorkflowController extends ApiController
{
    public function __construct(
        protected TicketWorkflowService $service
    ) {}

    /**
     * History Workflow
     */
    public function history(Ticket $ticket)
    {
        $data = $this->service->history($ticket);

        return $this->success(
            $data,
            'Workflow berhasil diambil'
        );
    }

    /**
     * Assign Teknisi
     */
    public function assign(
        AssignTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->assign(
            $ticket,
            $request->technicians,
            $request->leader_id,
            auth()->id() ?? 1
        );

        return $this->success(
            new TicketResource($ticket),
            'Teknisi berhasil ditugaskan'
        );
    }

    /**
     * Reassign Teknisi
     */
    public function reassign(
        ReassignTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->reassign(
            $ticket,
            $request->technicians,
            $request->leader_id,
            auth()->id() ?? 1
        );

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

    /**
     * Approve
     */
    public function approve(
        ApproveTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->approve(
            $ticket,
            $request->notes,
            auth()->id() ?? 1
        );

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

    /**
     * Reject
     */
    public function reject(
        RejectTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->reject(
            $ticket,
            $request->reason,
            auth()->id() ?? 1
        );

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

    /**
     * Start Work
     */
    public function start(
        StartTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->start(
            $ticket,
            $request->validated(),
            auth()->id() ?? 1
        );

        return $this->success(
            new TicketResource($ticket),
            'Pekerjaan dimulai'
        );
    }

    /**
     * Pending
     */
    public function pending(
        PendingTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->pending(
            $ticket,
            $request->reason,
            auth()->id() ?? 1
        );

        return $this->success(
            new TicketResource($ticket),
            'Ticket pending'
        );
    }

    /**
     * Resume
     */
    public function resume(Ticket $ticket)
    {
        $ticket = $this->service->resume(
            $ticket,
            auth()->id() ?? 1
        );

        return $this->success(
            new TicketResource($ticket),
            'Ticket dilanjutkan'
        );
    }

    /**
     * Resolve
     */
    public function resolve(
        ResolveTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->resolve(
            $ticket,
            $request->notes,
            auth()->id() ?? 1
        );

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

    /**
     * Close
     */
    public function close(
        CloseTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->close(
            $ticket,
            $request->notes,
            auth()->id() ?? 1
        );

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

    /**
     * Cancel
     */
    public function cancel(
        CancelTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->cancel(
            $ticket,
            $request->reason,
            auth()->id() ?? 1
        );

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

    /**
     * Escalate
     */
    public function escalate(
        EscalateTicketRequest $request,
        Ticket $ticket
    ) {
        $ticket = $this->service->escalate(
            $ticket,
            $request->reason,
            auth()->id() ?? 1
        );

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