<?php

namespace App\Services;

use App\Models\Popups\Popup;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class PopupService extends Service
{
    /**
     * 팝업 목록 조회 (관리자용)
     */
    public function index(array $params): LengthAwarePaginator
    {
        $tenantId = $this->tenantId();

        $query = Popup::query()
            ->where('tenant_id', $tenantId)
            ->with(['creator:id,name', 'department:id,name']);

        // 대상 유형 필터
        if (! empty($params['target_type'])) {
            $query->where('target_type', $params['target_type']);
        }

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

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

        // 정렬
        $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 getActivePopups(?int $departmentId = null): Collection
    {
        $tenantId = $this->tenantId();

        return Popup::query()
            ->where('tenant_id', $tenantId)
            ->active()
            ->forUser($departmentId)
            ->orderBy('created_at', 'desc')
            ->get();
    }

    /**
     * 팝업 상세 조회
     */
    public function show(int $id): Popup
    {
        $tenantId = $this->tenantId();

        return Popup::query()
            ->where('tenant_id', $tenantId)
            ->with(['creator:id,name', 'department:id,name'])
            ->findOrFail($id);
    }

    /**
     * 팝업 등록
     */
    public function store(array $data): Popup
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        $popup = new Popup;
        $popup->tenant_id = $tenantId;
        $popup->target_type = $data['target_type'] ?? Popup::TARGET_ALL;
        $popup->target_id = $data['target_id'] ?? null;
        $popup->title = $data['title'];
        $popup->content = $data['content'];
        $popup->status = $data['status'] ?? Popup::STATUS_INACTIVE;
        $popup->started_at = $data['started_at'] ?? null;
        $popup->ended_at = $data['ended_at'] ?? null;
        $popup->options = $data['options'] ?? null;
        $popup->created_by = $userId;
        $popup->save();

        return $popup->load(['creator:id,name', 'department:id,name']);
    }

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

        $popup = Popup::query()
            ->where('tenant_id', $tenantId)
            ->findOrFail($id);

        if (isset($data['target_type'])) {
            $popup->target_type = $data['target_type'];
        }
        if (array_key_exists('target_id', $data)) {
            $popup->target_id = $data['target_id'];
        }
        if (isset($data['title'])) {
            $popup->title = $data['title'];
        }
        if (isset($data['content'])) {
            $popup->content = $data['content'];
        }
        if (isset($data['status'])) {
            $popup->status = $data['status'];
        }
        if (array_key_exists('started_at', $data)) {
            $popup->started_at = $data['started_at'];
        }
        if (array_key_exists('ended_at', $data)) {
            $popup->ended_at = $data['ended_at'];
        }
        if (array_key_exists('options', $data)) {
            $popup->options = $data['options'];
        }

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

        return $popup->load(['creator:id,name', 'department:id,name']);
    }

    /**
     * 팝업 삭제
     */
    public function destroy(int $id): void
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        $popup = Popup::query()
            ->where('tenant_id', $tenantId)
            ->findOrFail($id);

        $popup->deleted_by = $userId;
        $popup->save();
        $popup->delete();
    }
}
