<?php

namespace App\Services;

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

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

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

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

        // 상태 필터
        if (! empty($params['status'])) {
            if ($params['status'] === 'receiving_pending') {
                // 입고대기: receiving_pending + inspection_pending
                $query->whereIn('status', ['receiving_pending', 'inspection_pending']);
            } elseif ($params['status'] === 'completed') {
                $query->where('status', 'completed');
            } else {
                $query->where('status', $params['status']);
            }
        }

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

        // 정렬
        $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 stats(): array
    {
        $tenantId = $this->tenantId();
        $today = now()->toDateString();

        $receivingPendingCount = Receiving::where('tenant_id', $tenantId)
            ->where('status', 'receiving_pending')
            ->count();

        $shippingCount = Receiving::where('tenant_id', $tenantId)
            ->where('status', 'shipping')
            ->count();

        $inspectionPendingCount = Receiving::where('tenant_id', $tenantId)
            ->where('status', 'inspection_pending')
            ->count();

        $todayReceivingCount = Receiving::where('tenant_id', $tenantId)
            ->where('status', 'completed')
            ->whereDate('receiving_date', $today)
            ->count();

        return [
            'receiving_pending_count' => $receivingPendingCount,
            'shipping_count' => $shippingCount,
            'inspection_pending_count' => $inspectionPendingCount,
            'today_receiving_count' => $todayReceivingCount,
        ];
    }

    /**
     * 입고 상세 조회
     */
    public function show(int $id): Receiving
    {
        $tenantId = $this->tenantId();

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

    /**
     * 입고 등록
     */
    public function store(array $data): Receiving
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

        return DB::transaction(function () use ($data, $tenantId, $userId) {
            // 입고번호 자동 생성
            $receivingNumber = $this->generateReceivingNumber($tenantId);

            $receiving = new Receiving;
            $receiving->tenant_id = $tenantId;
            $receiving->receiving_number = $receivingNumber;
            $receiving->order_no = $data['order_no'] ?? null;
            $receiving->order_date = $data['order_date'] ?? null;
            $receiving->item_id = $data['item_id'] ?? null;
            $receiving->item_code = $data['item_code'];
            $receiving->item_name = $data['item_name'];
            $receiving->specification = $data['specification'] ?? null;
            $receiving->supplier = $data['supplier'];
            $receiving->order_qty = $data['order_qty'];
            $receiving->order_unit = $data['order_unit'] ?? 'EA';
            $receiving->due_date = $data['due_date'] ?? null;
            $receiving->status = $data['status'] ?? 'order_completed';
            $receiving->remark = $data['remark'] ?? null;
            $receiving->created_by = $userId;
            $receiving->updated_by = $userId;
            $receiving->save();

            return $receiving;
        });
    }

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

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

            if (! $receiving->canEdit()) {
                throw new \Exception(__('error.receiving.cannot_edit'));
            }

            if (isset($data['order_no'])) {
                $receiving->order_no = $data['order_no'];
            }
            if (isset($data['order_date'])) {
                $receiving->order_date = $data['order_date'];
            }
            if (isset($data['item_code'])) {
                $receiving->item_code = $data['item_code'];
            }
            if (isset($data['item_name'])) {
                $receiving->item_name = $data['item_name'];
            }
            if (array_key_exists('specification', $data)) {
                $receiving->specification = $data['specification'];
            }
            if (isset($data['supplier'])) {
                $receiving->supplier = $data['supplier'];
            }
            if (isset($data['order_qty'])) {
                $receiving->order_qty = $data['order_qty'];
            }
            if (isset($data['order_unit'])) {
                $receiving->order_unit = $data['order_unit'];
            }
            if (array_key_exists('due_date', $data)) {
                $receiving->due_date = $data['due_date'];
            }
            if (isset($data['status'])) {
                $receiving->status = $data['status'];
            }
            if (array_key_exists('remark', $data)) {
                $receiving->remark = $data['remark'];
            }

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

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

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

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

            if (! $receiving->canDelete()) {
                throw new \Exception(__('error.receiving.cannot_delete'));
            }

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

            return true;
        });
    }

    /**
     * 입고처리 (상태 변경 + 입고 정보 입력)
     */
    public function process(int $id, array $data): Receiving
    {
        $tenantId = $this->tenantId();
        $userId = $this->apiUserId();

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

            if (! $receiving->canProcess()) {
                throw new \Exception(__('error.receiving.cannot_process'));
            }

            // LOT번호 생성 (없으면 자동 생성)
            $lotNo = $data['lot_no'] ?? $this->generateLotNo();

            $receiving->receiving_qty = $data['receiving_qty'];
            $receiving->receiving_date = $data['receiving_date'] ?? now()->toDateString();
            $receiving->lot_no = $lotNo;
            $receiving->supplier_lot = $data['supplier_lot'] ?? null;
            $receiving->receiving_location = $data['receiving_location'];
            $receiving->receiving_manager = $data['receiving_manager'] ?? null;
            $receiving->status = 'completed';
            $receiving->remark = $data['remark'] ?? $receiving->remark;
            $receiving->updated_by = $userId;
            $receiving->save();

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

    /**
     * 입고번호 자동 생성
     */
    private function generateReceivingNumber(int $tenantId): string
    {
        $prefix = 'RV'.date('Ymd');

        $lastReceiving = Receiving::query()
            ->where('tenant_id', $tenantId)
            ->where('receiving_number', 'like', $prefix.'%')
            ->orderBy('receiving_number', 'desc')
            ->first();

        if ($lastReceiving) {
            $lastSeq = (int) substr($lastReceiving->receiving_number, -4);
            $newSeq = $lastSeq + 1;
        } else {
            $newSeq = 1;
        }

        return $prefix.str_pad($newSeq, 4, '0', STR_PAD_LEFT);
    }

    /**
     * LOT번호 자동 생성
     */
    private function generateLotNo(): string
    {
        $now = now();
        $year = $now->format('y');
        $month = $now->format('m');
        $day = $now->format('d');
        $seq = str_pad(rand(1, 99), 2, '0', STR_PAD_LEFT);

        return "{$year}{$month}{$day}-{$seq}";
    }
}
