<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;

abstract class Controller
{
    /**
     * 成功响应
     */
    protected function success($data = null, string $message = 'success'): JsonResponse
    {
        return response()->json([
            'code' => 0,
            'message' => $message,
            'data' => $data,
        ]);
    }

    /**
     * 分页响应
     */
    protected function paginate($paginator): JsonResponse
    {
        return response()->json([
            'code' => 0,
            'data' => [
                'list' => $paginator->items(),
                'total' => $paginator->total(),
                'page' => $paginator->currentPage(),
                'page_size' => $paginator->perPage(),
            ],
        ]);
    }

    /**
     * 错误响应
     */
    protected function error(string $message, int $code = 40001, int $httpCode = 400): JsonResponse
    {
        return response()->json([
            'code' => $code,
            'message' => $message,
        ], $httpCode);
    }
}
