<?php

namespace App\Http\Controllers\Finance;

use App\Http\Controllers\Controller;
use App\Models\Finance\Customer;
use App\Services\TradingPartnerOcrService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CustomerController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $tenantId = session('selected_tenant_id', 1);
        $query = Customer::forTenant($tenantId);

        if ($search = $request->input('search')) {
            $query->where(function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                    ->orWhere('ceo', 'like', "%{$search}%")
                    ->orWhere('manager', 'like', "%{$search}%");
            });
        }
        if ($status = $request->input('status')) {
            if ($status !== 'all') {
                $query->where('status', $status);
            }
        }
        if ($grade = $request->input('grade')) {
            if ($grade !== 'all') {
                $query->where('grade', $grade);
            }
        }

        $customers = $query->orderBy('created_at', 'desc')->get()->map(fn ($item) => [
            'id' => $item->id, 'name' => $item->name, 'bizNo' => $item->biz_no,
            'ceo' => $item->ceo, 'industry' => $item->industry, 'grade' => $item->grade,
            'contact' => $item->contact, 'email' => $item->email, 'address' => $item->address,
            'manager' => $item->manager, 'managerPhone' => $item->manager_phone,
            'status' => $item->status, 'memo' => $item->memo,
        ]);

        $all = Customer::forTenant($tenantId)->get();
        $stats = [
            'total' => $all->count(),
            'active' => $all->where('status', 'active')->count(),
            'vip' => $all->where('grade', 'VIP')->count(),
            'inactive' => $all->where('status', 'inactive')->count(),
        ];

        return response()->json(['success' => true, 'data' => $customers, 'stats' => $stats]);
    }

    public function store(Request $request): JsonResponse
    {
        $request->validate(['name' => 'required|string|max:100']);
        $tenantId = session('selected_tenant_id', 1);

        Customer::create([
            'tenant_id' => $tenantId, 'name' => $request->input('name'),
            'biz_no' => $request->input('bizNo'), 'ceo' => $request->input('ceo'),
            'industry' => $request->input('industry'), 'grade' => $request->input('grade', 'Silver'),
            'contact' => $request->input('contact'), 'email' => $request->input('email'),
            'address' => $request->input('address'), 'manager' => $request->input('manager'),
            'manager_phone' => $request->input('managerPhone'),
            'status' => $request->input('status', 'active'), 'memo' => $request->input('memo'),
        ]);

        return response()->json(['success' => true, 'message' => '고객사가 등록되었습니다.']);
    }

    public function update(Request $request, int $id): JsonResponse
    {
        $tenantId = session('selected_tenant_id', 1);
        $item = Customer::forTenant($tenantId)->findOrFail($id);
        $request->validate(['name' => 'required|string|max:100']);

        $item->update([
            'name' => $request->input('name'), 'biz_no' => $request->input('bizNo'),
            'ceo' => $request->input('ceo'), 'industry' => $request->input('industry'),
            'grade' => $request->input('grade', $item->grade),
            'contact' => $request->input('contact'), 'email' => $request->input('email'),
            'address' => $request->input('address'), 'manager' => $request->input('manager'),
            'manager_phone' => $request->input('managerPhone'),
            'status' => $request->input('status', $item->status), 'memo' => $request->input('memo'),
        ]);

        return response()->json(['success' => true, 'message' => '고객사가 수정되었습니다.']);
    }

    public function destroy(int $id): JsonResponse
    {
        $tenantId = session('selected_tenant_id', 1);
        Customer::forTenant($tenantId)->findOrFail($id)->delete();

        return response()->json(['success' => true, 'message' => '고객사가 삭제되었습니다.']);
    }

    public function ocr(Request $request, TradingPartnerOcrService $ocrService): JsonResponse
    {
        $request->validate([
            'image' => [
                'required',
                'string',
                function ($attribute, $value, $fail) {
                    if (strlen($value) > 7 * 1024 * 1024) {
                        $fail('이미지 크기는 5MB 이하여야 합니다.');
                    }
                },
            ],
        ]);

        try {
            $result = $ocrService->extractFromImage($request->input('image'));

            // raw_response에서 원본 OCR 데이터 파싱 (address, biz_type 등 직접 접근)
            $raw = json_decode($result['raw_response'], true) ?? [];

            // 고객사 폼 필드에 맞게 매핑
            $data = [
                'name' => trim($raw['company_name'] ?? $result['data']['name'] ?? ''),
                'bizNo' => $result['data']['bizNo'] ?? '',
                'ceo' => trim($raw['ceo_name'] ?? $result['data']['manager'] ?? ''),
                'contact' => $result['data']['contact'] ?? '',
                'email' => $result['data']['email'] ?? '',
                'address' => trim($raw['address'] ?? ''),
                'industry' => $this->matchIndustry($raw['biz_type'] ?? '', $raw['biz_item'] ?? ''),
                'memo' => $this->buildCustomerMemo($raw),
            ];

            return response()->json(['ok' => true, 'data' => $data]);
        } catch (\RuntimeException $e) {
            return response()->json(['ok' => false, 'message' => $e->getMessage()], 500);
        } catch (\Throwable $e) {
            Log::error('고객사 OCR 예상치 못한 오류', ['error' => $e->getMessage()]);

            return response()->json(['ok' => false, 'message' => 'OCR 처리 중 오류가 발생했습니다.'], 500);
        }
    }

    private function matchIndustry(string $bizType, string $bizItem): string
    {
        $text = $bizType.' '.$bizItem;

        $keywords = [
            'IT/소프트웨어' => ['소프트웨어', 'IT', '정보통신', '전산', '컴퓨터', '인터넷', '데이터', '프로그램'],
            '제조업' => ['제조', '생산', '가공', '조립'],
            '서비스업' => ['서비스', '용역', '컨설팅', '대행'],
            '유통업' => ['유통', '도매', '소매', '판매', '무역', '수출', '수입', '상업'],
            '금융업' => ['금융', '보험', '은행', '증권', '투자'],
        ];

        foreach ($keywords as $industry => $kws) {
            foreach ($kws as $kw) {
                if (mb_strpos($text, $kw) !== false) {
                    return $industry;
                }
            }
        }

        return '기타';
    }

    private function buildCustomerMemo(array $raw): string
    {
        $parts = [];
        $bizType = trim($raw['biz_type'] ?? '');
        $bizItem = trim($raw['biz_item'] ?? '');
        if ($bizType) {
            $parts[] = "[업태] {$bizType}";
        }
        if ($bizItem) {
            $parts[] = "[종목] {$bizItem}";
        }

        return implode(' / ', $parts);
    }
}
