<?php

namespace App\Http\Controllers\System\Admin;

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

class Config extends Controller
{
    protected $configService;

    public function __construct(ConfigService $configService)
    {
        $this->configService = $configService;
    }

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

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

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

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

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

    public function destroy(int $id)
    {
        try {
            $this->configService->delete($id);
            return response()->json([
                'code' => 200,
                'message' => '删除成功',
                'data' => null
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

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

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

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

    public function getByGroup(Request $request)
    {
        $group = $request->input('group');
        // 如果没有指定分组，返回所有配置项
        if (empty($group)) {
            $result = $this->configService->getList([]);
            $configs = $result['list'] ?? [];
        } else {
            $configs = $this->configService->getByGroup($group);
        }
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $configs
        ]);
    }
}
