<?php

namespace App\Http\Controllers\System\Admin;

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

class City extends Controller
{
    protected $cityService;

    public function __construct(CityService $cityService)
    {
        $this->cityService = $cityService;
    }

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

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

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

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

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

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

    public function destroy(int $id)
    {
        try {
            $this->cityService->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->cityService->batchDelete($request->input('ids', []));
        return response()->json([
            'code' => 200,
            'message' => '批量删除成功',
            'data' => null
        ]);
    }

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

    public function children(string $parentCode)
    {
        $children = $this->cityService->getChildren($parentCode);
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $children
        ]);
    }

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

    public function cities(int $provinceId)
    {
        $cities = $this->cityService->getCities($provinceId);
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $cities
        ]);
    }

    public function districts(int $cityId)
    {
        $districts = $this->cityService->getDistricts($cityId);
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $districts
        ]);
    }
}
