<?php

namespace App\Services;

use App\Models\Tenants\SiteBriefing;
use App\Services\Quote\QuoteService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;

class SiteBriefingService extends Service
{
    public function __construct(
        private QuoteService $quoteService
    ) {}

    /**
     * 현장설명회 목록 조회
     */
    public function index(array $params): LengthAwarePaginator
    {
        $tenantId = $this->tenantId();

        $query = SiteBriefing::query()
            ->with(['partner:id,name', 'site:id,name'])
            ->where('tenant_id', $tenantId);

        // 검색
        if (! empty($params['search'])) {
            $search = $params['search'];
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%")
                    ->orWhere('briefing_code', 'like', "%{$search}%")
                    ->orWhere('location', 'like', "%{$search}%")
                    ->orWhereHas('partner', function ($pq) use ($search) {
                        $pq->where('name', 'like', "%{$search}%");
                    });
            });
        }

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

        // 입찰상태 필터
        if (! empty($params['bid_status']) && $params['bid_status'] !== 'all') {
            $query->where('bid_status', $params['bid_status']);
        }

        // 거래처 필터
        if (! empty($params['partner_id'])) {
            $query->where('partner_id', $params['partner_id']);
        }

        // 현장 필터
        if (! empty($params['site_id'])) {
            $query->where('site_id', $params['site_id']);
        }

        // 날짜 범위 필터
        if (! empty($params['start_date'])) {
            $query->where('briefing_date', '>=', $params['start_date']);
        }
        if (! empty($params['end_date'])) {
            $query->where('briefing_date', '<=', $params['end_date']);
        }

        // 정렬
        $sortBy = $params['sort_by'] ?? 'created_at';
        $sortDir = $params['sort_dir'] ?? 'desc';

        $sortMapping = [
            'created_at' => 'created_at',
            'briefing_date' => 'briefing_date',
            'title' => 'title',
        ];

        $sortColumn = $sortMapping[$sortBy] ?? 'created_at';
        $query->orderBy($sortColumn, $sortDir);

        $perPage = min($params['per_page'] ?? 20, 100);

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

    /**
     * 현장설명회 상세 조회
     */
    public function show(int $id): SiteBriefing
    {
        return SiteBriefing::query()
            ->with(['partner:id,name', 'site:id,name,address', 'creator:id,name'])
            ->where('tenant_id', $this->tenantId())
            ->findOrFail($id);
    }

    /**
     * 현장설명회 등록
     */
    public function store(array $data): SiteBriefing
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        return DB::transaction(function () use ($data, $tenantId, $userId) {
            // 현설번호 자동 생성
            $briefingCode = SiteBriefing::generateBriefingCode($tenantId);

            // 참석자 배열 처리
            $attendees = $data['attendees'] ?? null;
            $attendeeCount = is_array($attendees) ? count($attendees) : 0;

            $siteBriefing = SiteBriefing::create([
                'tenant_id' => $tenantId,
                'briefing_code' => $briefingCode,
                'title' => $data['title'],
                'description' => $data['description'] ?? null,
                'partner_id' => $data['partner_id'] ?? null,
                'site_id' => $data['site_id'] ?? null,
                'briefing_date' => $data['briefing_date'],
                'briefing_time' => $data['briefing_time'] ?? null,
                'briefing_type' => $data['briefing_type'] ?? SiteBriefing::TYPE_OFFLINE,
                'location' => $data['location'] ?? null,
                'address' => $data['address'] ?? null,
                'status' => $data['status'] ?? SiteBriefing::STATUS_SCHEDULED,
                'bid_status' => $data['bid_status'] ?? SiteBriefing::BID_STATUS_PENDING,
                'bid_date' => $data['bid_date'] ?? null,
                'attendees' => $attendees,
                'attendee_count' => $attendeeCount,
                'attendance_status' => $data['attendance_status'] ?? SiteBriefing::ATTENDANCE_SCHEDULED,
                'site_count' => $data['site_count'] ?? 0,
                'construction_start_date' => $data['construction_start_date'] ?? null,
                'construction_end_date' => $data['construction_end_date'] ?? null,
                'vat_type' => $data['vat_type'] ?? SiteBriefing::VAT_EXCLUDED,
                'created_by' => $userId,
            ]);

            return $siteBriefing->load(['partner:id,name', 'site:id,name']);
        });
    }

    /**
     * 현장설명회 수정
     *
     * attendance_status가 'attended'(참석완료) 상태면 견적을 upsert합니다.
     * - 견적이 없으면: 신규 생성
     * - 견적이 있으면: 거래처, 현장 정보 등 동기화
     */
    public function update(int $id, array $data): SiteBriefing
    {
        $userId = $this->apiUserId();

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

            // 저장 후 참석완료 상태인지 확인
            $newAttendanceStatus = $data['attendance_status'] ?? $siteBriefing->attendance_status;
            $isAttended = $newAttendanceStatus === SiteBriefing::ATTENDANCE_ATTENDED;

            $updateData = array_filter([
                'title' => $data['title'] ?? null,
                'description' => $data['description'] ?? null,
                'partner_id' => $data['partner_id'] ?? null,
                'site_id' => $data['site_id'] ?? null,
                'briefing_date' => $data['briefing_date'] ?? null,
                'briefing_time' => $data['briefing_time'] ?? null,
                'briefing_type' => $data['briefing_type'] ?? null,
                'location' => $data['location'] ?? null,
                'address' => $data['address'] ?? null,
                'status' => $data['status'] ?? null,
                'bid_status' => $data['bid_status'] ?? null,
                'bid_date' => $data['bid_date'] ?? null,
                'attendance_status' => $data['attendance_status'] ?? null,
                'site_count' => $data['site_count'] ?? null,
                'construction_start_date' => $data['construction_start_date'] ?? null,
                'construction_end_date' => $data['construction_end_date'] ?? null,
                'vat_type' => $data['vat_type'] ?? null,
            ], fn ($v) => $v !== null);

            // 참석자 배열 처리 (명시적으로 전달된 경우에만 업데이트)
            if (array_key_exists('attendees', $data)) {
                $attendees = $data['attendees'];
                $updateData['attendees'] = $attendees;
                $updateData['attendee_count'] = is_array($attendees) ? count($attendees) : 0;
            }

            $updateData['updated_by'] = $userId;

            $siteBriefing->update($updateData);

            // 참석완료 상태면 견적 upsert (신규 생성 또는 정보 동기화)
            if ($isAttended) {
                $siteBriefing->load('partner'); // partner 정보 로드 (견적에 필요)
                $this->quoteService->upsertFromSiteBriefing($siteBriefing);
            }

            return $siteBriefing->load(['partner:id,name', 'site:id,name']);
        });
    }

    /**
     * 현장설명회 삭제
     */
    public function destroy(int $id): bool
    {
        return DB::transaction(function () use ($id) {
            $siteBriefing = SiteBriefing::query()
                ->where('tenant_id', $this->tenantId())
                ->findOrFail($id);

            $siteBriefing->update(['deleted_by' => $this->apiUserId()]);

            return $siteBriefing->delete();
        });
    }

    /**
     * 현장설명회 일괄 삭제
     */
    public function bulkDestroy(array $ids): int
    {
        return DB::transaction(function () use ($ids) {
            $userId = $this->apiUserId();
            $tenantId = $this->tenantId();

            $siteBriefings = SiteBriefing::query()
                ->where('tenant_id', $tenantId)
                ->whereIn('id', $ids)
                ->get();

            foreach ($siteBriefings as $siteBriefing) {
                $siteBriefing->update(['deleted_by' => $userId]);
                $siteBriefing->delete();
            }

            return $siteBriefings->count();
        });
    }

    /**
     * 현장설명회 통계 조회
     */
    public function stats(): array
    {
        $tenantId = $this->tenantId();

        $stats = SiteBriefing::query()
            ->where('tenant_id', $tenantId)
            ->selectRaw("
                COUNT(*) as total,
                SUM(CASE WHEN status = 'scheduled' THEN 1 ELSE 0 END) as scheduled,
                SUM(CASE WHEN status = 'ongoing' THEN 1 ELSE 0 END) as ongoing,
                SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed,
                SUM(CASE WHEN status IN ('cancelled', 'postponed') THEN 1 ELSE 0 END) as cancelled
            ")
            ->first();

        return [
            'total' => (int) $stats->total,
            'scheduled' => (int) $stats->scheduled,
            'ongoing' => (int) $stats->ongoing,
            'completed' => (int) $stats->completed,
            'cancelled' => (int) $stats->cancelled,
        ];
    }
}
