<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CommonController
{
    public static function getComeCode()
    {
        return ApiResponse::handle(function () {
            return DB::table('common_codes')
                ->select(['code_group', 'code', 'name', 'description', 'is_active'])
                ->where('tenant_id', app('tenant_id'))
                ->get();
        }, '공통코드');
    }

    public function list(Request $request)
    {
        return ApiResponse::handle(function () {
            // Service implementation needed
            return [];
        }, __('message.fetched'));
    }

    /**
     * 특정 그룹의 공통 코드 목록 조회
     *
     * GET /api/v1/settings/common/{group}
     */
    public function index(Request $request, string $group)
    {
        return ApiResponse::handle(function () use ($group) {
            $tenantId = app('tenant_id');

            return DB::table('common_codes')
                ->select(['id', 'code', 'name', 'description', 'sort_order', 'attributes'])
                ->where('code_group', $group)
                ->where('is_active', true)
                ->where(function ($query) use ($tenantId) {
                    $query->where('tenant_id', $tenantId)
                        ->orWhereNull('tenant_id');
                })
                ->orderBy('sort_order')
                ->get();
        }, __('message.fetched'));
    }

    public function store(Request $request)
    {
        return ApiResponse::handle(function () {
            // Service implementation needed
            return [];
        }, __('message.settings.common_code_saved'));
    }

    public function update(Request $request, int $id)
    {
        return ApiResponse::handle(function () {
            // Service implementation needed
            return [];
        }, __('message.updated'));
    }

    public function destroy(Request $request, int $id)
    {
        return ApiResponse::handle(function () {
            // Service implementation needed
            return [];
        }, __('message.deleted'));
    }
}
