<?php

namespace App\Http\Controllers\System\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\System\TaskService;

class Task extends Controller
{
    protected $taskService;

    public function __construct(TaskService $taskService)
    {
        $this->taskService = $taskService;
    }

    public function index(Request $request)
    {
        $result = $this->taskService->getList($request->all());
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $result
        ]);
    }

    public function all()
    {
        $tasks = $this->taskService->getAll();
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $tasks
        ]);
    }

    public function show(int $id)
    {
        $task = $this->taskService->getById($id);
        if (!$task) {
            return response()->json([
                'code' => 404,
                'message' => '任务不存在',
                'data' => null
            ], 404);
        }

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $task
        ]);
    }

    public function store(Request $request)
    {
        try {
            $task = $this->taskService->create($request->all());
            return response()->json([
                'code' => 200,
                'message' => '创建成功',
                'data' => $task
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => null
            ], 422);
        }
    }

    public function update(Request $request, int $id)
    {
        try {
            $task = $this->taskService->update($id, $request->all());
            return response()->json([
                'code' => 200,
                'message' => '更新成功',
                'data' => $task
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => null
            ], 422);
        }
    }

    public function destroy(int $id)
    {
        $this->taskService->delete($id);
        return response()->json([
            'code' => 200,
            'message' => '删除成功',
            'data' => null
        ]);
    }

    public function batchDelete(Request $request)
    {
        $this->taskService->batchDelete($request->input('ids', []));
        return response()->json([
            'code' => 200,
            'message' => '批量删除成功',
            'data' => null
        ]);
    }

    public function batchUpdateStatus(Request $request)
    {
        $this->taskService->batchUpdateStatus(
            $request->input('ids', []),
            $request->input('status', true)
        );
        return response()->json([
            'code' => 200,
            'message' => '批量更新状态成功',
            'data' => null
        ]);
    }

    public function run(int $id)
    {
        try {
            $result = $this->taskService->run($id);
            return response()->json([
                'code' => 200,
                'message' => '执行成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

    public function getStatistics()
    {
        $statistics = $this->taskService->getStatistics();
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $statistics
        ]);
    }
}
