<?php

namespace App\Services;

use App\Models\Orders\ClientGroup;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ClientGroupService extends Service
{
    /** 목록(검색/페이징) */
    public function index(array $params)
    {
        $tenantId = $this->tenantId();

        $page = (int) ($params['page'] ?? 1);
        $size = (int) ($params['size'] ?? 20);
        $q = trim((string) ($params['q'] ?? ''));
        $onlyActive = $params['only_active'] ?? null;

        $query = ClientGroup::query()->where('tenant_id', $tenantId);

        if ($q !== '') {
            $query->where(function ($qq) use ($q) {
                $qq->where('group_name', 'like', "%{$q}%")
                    ->orWhere('group_code', 'like', "%{$q}%");
            });
        }

        if ($onlyActive !== null) {
            $query->where('is_active', $onlyActive ? 1 : 0);
        }

        $query->orderBy('group_code')->orderBy('id');

        return $query->paginate($size, ['*'], 'page', $page);
    }

    /** 단건 */
    public function show(int $id)
    {
        $tenantId = $this->tenantId();
        $group = ClientGroup::where('tenant_id', $tenantId)->find($id);
        if (! $group) {
            throw new NotFoundHttpException(__('error.not_found'));
        }

        return $group;
    }

    /** 생성 */
    public function store(array $params)
    {
        $tenantId = $this->tenantId();
        $uid = $this->apiUserId();

        $v = Validator::make($params, [
            'group_code' => 'required|string|max:30',
            'group_name' => 'required|string|max:100',
            'price_rate' => 'required|numeric|min:0|max:99.9999',
            'is_active' => 'nullable|boolean',
        ]);

        if ($v->fails()) {
            throw new BadRequestHttpException($v->errors()->first());
        }

        $data = $v->validated();

        // group_code 중복 검사 (삭제된 레코드 포함)
        $existing = ClientGroup::withTrashed()
            ->where('tenant_id', $tenantId)
            ->where('group_code', $data['group_code'])
            ->first();

        // 삭제된 레코드가 있으면 복원하고 업데이트
        if ($existing && $existing->trashed()) {
            $existing->restore();
            $existing->update([
                'group_name' => $data['group_name'],
                'price_rate' => $data['price_rate'],
                'is_active' => $data['is_active'] ?? 1,
                'updated_by' => $uid,
            ]);

            return $existing->refresh();
        }

        // 활성 레코드가 이미 있으면 에러
        if ($existing) {
            throw new BadRequestHttpException(__('error.duplicate_code'));
        }

        $data['tenant_id'] = $tenantId;
        $data['is_active'] = $data['is_active'] ?? 1;
        $data['created_by'] = $uid;

        return ClientGroup::create($data);
    }

    /** 수정 */
    public function update(int $id, array $params)
    {
        $tenantId = $this->tenantId();
        $uid = $this->apiUserId();

        $group = ClientGroup::where('tenant_id', $tenantId)->find($id);
        if (! $group) {
            throw new NotFoundHttpException(__('error.not_found'));
        }

        $v = Validator::make($params, [
            'group_code' => 'sometimes|required|string|max:30',
            'group_name' => 'sometimes|required|string|max:100',
            'price_rate' => 'sometimes|required|numeric|min:0|max:99.9999',
            'is_active' => 'nullable|boolean',
        ]);

        if ($v->fails()) {
            throw new BadRequestHttpException($v->errors()->first());
        }

        $payload = $v->validated();

        // group_code 변경 시 중복 검사 (삭제된 레코드 포함)
        if (isset($payload['group_code']) && $payload['group_code'] !== $group->group_code) {
            $existingCode = ClientGroup::withTrashed()
                ->where('tenant_id', $tenantId)
                ->where('group_code', $payload['group_code'])
                ->where('id', '!=', $id) // 자기 자신 제외
                ->first();

            // 삭제된 레코드가 있으면 에러 (update는 복원하지 않음)
            if ($existingCode && $existingCode->trashed()) {
                throw new BadRequestHttpException(__('error.code_exists_in_deleted'));
            }

            // 활성 레코드가 있으면 에러
            if ($existingCode) {
                throw new BadRequestHttpException(__('error.duplicate_code'));
            }
        }

        $payload['updated_by'] = $uid;

        $group->update($payload);

        return $group->refresh();
    }

    /** 삭제 */
    public function destroy(int $id)
    {
        $tenantId = $this->tenantId();
        $uid = $this->apiUserId();

        $group = ClientGroup::where('tenant_id', $tenantId)->find($id);
        if (! $group) {
            throw new NotFoundHttpException(__('error.not_found'));
        }

        // 해당 그룹에 속한 고객이 있는지 검사
        if ($group->clients()->exists()) {
            throw new BadRequestHttpException(__('error.has_clients'));
        }

        $group->deleted_by = $uid;
        $group->save();
        $group->delete();

        return 'success';
    }

    /** 활성/비활성 토글 */
    public function toggle(int $id)
    {
        $tenantId = $this->tenantId();
        $uid = $this->apiUserId();

        $group = ClientGroup::where('tenant_id', $tenantId)->find($id);
        if (! $group) {
            throw new NotFoundHttpException(__('error.not_found'));
        }

        $group->is_active = $group->is_active ? 0 : 1;
        $group->updated_by = $uid;
        $group->save();

        return $group->refresh();
    }
}
