<?php

namespace App\Http\Controllers\System\Api;

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 tree()
    {
        $tree = $this->cityService->getCachedTree();
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $tree
        ]);
    }

    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
        ]);
    }

    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
        ]);
    }
}
