<?php

namespace App\Services;

use App\Models\Tenants\Card;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;

class CardService extends Service
{
    /**
     * 카드 목록 조회
     */
    public function index(array $params): LengthAwarePaginator
    {
        $tenantId = $this->tenantId();

        $query = Card::query()
            ->where('tenant_id', $tenantId)
            ->with([
                'assignedUser:id,name,email',
                'assignedUser.tenantProfiles' => function ($q) use ($tenantId) {
                    $q->where('tenant_id', $tenantId)
                        ->with('department:id,name');
                },
            ]);

        // 검색 필터
        if (! empty($params['search'])) {
            $search = $params['search'];
            $query->where(function ($q) use ($search) {
                $q->where('card_name', 'like', "%{$search}%")
                    ->orWhere('card_company', 'like', "%{$search}%")
                    ->orWhere('card_number_last4', 'like', "%{$search}%");
            });
        }

        // 상태 필터
        if (! empty($params['status'])) {
            $query->where('status', $params['status']);
        }

        // 담당자 필터
        if (! empty($params['assigned_user_id'])) {
            $query->where('assigned_user_id', $params['assigned_user_id']);
        }

        // 정렬
        $sortBy = $params['sort_by'] ?? 'created_at';
        $sortDir = $params['sort_dir'] ?? 'desc';
        $query->orderBy($sortBy, $sortDir);

        // 페이지네이션
        $perPage = $params['per_page'] ?? 20;

        return $query->paginate($perPage);
    }

    /**
     * 카드 상세 조회
     */
    public function show(int $id): Card
    {
        $tenantId = $this->tenantId();

        return Card::query()
            ->where('tenant_id', $tenantId)
            ->with([
                'assignedUser:id,name,email',
                'assignedUser.tenantProfiles' => function ($q) use ($tenantId) {
                    $q->where('tenant_id', $tenantId)
                        ->with('department:id,name');
                },
            ])
            ->findOrFail($id);
    }

    /**
     * 카드 등록
     */
    public function store(array $data): Card
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        return DB::transaction(function () use ($data, $tenantId, $userId) {
            $card = new Card;
            $card->tenant_id = $tenantId;
            $card->card_company = $data['card_company'];
            $card->setCardNumber($data['card_number']);
            $card->expiry_date = $data['expiry_date'];
            $card->card_name = $data['card_name'];
            $card->status = $data['status'] ?? 'active';
            $card->assigned_user_id = $data['assigned_user_id'] ?? null;
            $card->created_by = $userId;
            $card->updated_by = $userId;

            if (! empty($data['card_password'])) {
                $card->setCardPassword($data['card_password']);
            }

            $card->save();

            return $card;
        });
    }

    /**
     * 카드 수정
     */
    public function update(int $id, array $data): Card
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        return DB::transaction(function () use ($id, $data, $tenantId, $userId) {
            $card = Card::query()
                ->where('tenant_id', $tenantId)
                ->findOrFail($id);

            if (isset($data['card_company'])) {
                $card->card_company = $data['card_company'];
            }
            if (isset($data['card_number'])) {
                $card->setCardNumber($data['card_number']);
            }
            if (isset($data['expiry_date'])) {
                $card->expiry_date = $data['expiry_date'];
            }
            if (isset($data['card_name'])) {
                $card->card_name = $data['card_name'];
            }
            if (isset($data['status'])) {
                $card->status = $data['status'];
            }
            if (array_key_exists('assigned_user_id', $data)) {
                $card->assigned_user_id = $data['assigned_user_id'];
            }
            if (isset($data['card_password'])) {
                $card->setCardPassword($data['card_password']);
            }

            $card->updated_by = $userId;
            $card->save();

            return $card->fresh();
        });
    }

    /**
     * 카드 삭제
     */
    public function destroy(int $id): bool
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        return DB::transaction(function () use ($id, $tenantId, $userId) {
            $card = Card::query()
                ->where('tenant_id', $tenantId)
                ->findOrFail($id);

            $card->deleted_by = $userId;
            $card->save();
            $card->delete();

            return true;
        });
    }

    /**
     * 카드 상태 토글 (사용/정지)
     */
    public function toggleStatus(int $id): Card
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        return DB::transaction(function () use ($id, $tenantId, $userId) {
            $card = Card::query()
                ->where('tenant_id', $tenantId)
                ->findOrFail($id);

            $card->toggleStatus();
            $card->updated_by = $userId;
            $card->save();

            return $card;
        });
    }

    /**
     * 활성 카드 목록 조회 (셀렉트박스용)
     */
    public function getActiveCards(): array
    {
        $tenantId = $this->tenantId();

        return Card::query()
            ->where('tenant_id', $tenantId)
            ->where('status', 'active')
            ->orderBy('card_name')
            ->get(['id', 'card_name', 'card_company', 'card_number_last4'])
            ->map(function ($card) {
                return [
                    'id' => $card->id,
                    'card_name' => $card->card_name,
                    'card_company' => $card->card_company,
                    'display_number' => '****-'.$card->card_number_last4,
                ];
            })
            ->toArray();
    }
}
