<?php

namespace App\Http\Controllers\Sales;

use App\Http\Controllers\Controller;
use App\Models\Sales\SalesContractProduct;
use App\Models\Sales\SalesTenantManagement;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 영업 계약관리 컨트롤러
 */
class SalesContractController extends Controller
{
    /**
     * 계약 상품 저장 (전체 교체 방식)
     * - tenant_id 또는 prospect_id 중 하나 필수
     */
    public function saveProducts(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'tenant_id' => 'nullable|exists:tenants,id',
            'prospect_id' => 'nullable|exists:tenant_prospects,id',
            'management_id' => 'nullable|exists:sales_tenant_managements,id',
            'category_id' => 'required|exists:sales_product_categories,id',
            'products' => 'required|array',
            'products.*.product_id' => 'required|exists:sales_products,id',
            'products.*.category_id' => 'required|exists:sales_product_categories,id',
            'products.*.registration_fee' => 'required|numeric|min:0',
            'products.*.subscription_fee' => 'required|numeric|min:0',
        ]);

        // tenant_id 또는 prospect_id 중 하나는 필수
        if (empty($validated['tenant_id']) && empty($validated['prospect_id'])) {
            return response()->json([
                'success' => false,
                'message' => 'tenant_id 또는 prospect_id가 필요합니다.',
            ], 422);
        }

        try {
            $managementId = null;

            DB::transaction(function () use ($validated, &$managementId) {
                $tenantId = $validated['tenant_id'] ?? null;
                $prospectId = $validated['prospect_id'] ?? null;
                $categoryId = $validated['category_id'];

                // 영업관리 레코드 조회 (없으면 생성)
                if ($tenantId) {
                    $management = SalesTenantManagement::findOrCreateByTenant($tenantId);
                } else {
                    $management = SalesTenantManagement::findOrCreateByProspect($prospectId);
                }
                $managementId = $management->id;

                // 해당 카테고리의 기존 상품 삭제
                SalesContractProduct::where('management_id', $management->id)
                    ->where('category_id', $categoryId)
                    ->delete();

                // 새 상품 저장
                foreach ($validated['products'] as $product) {
                    SalesContractProduct::create([
                        'tenant_id' => $tenantId,
                        'management_id' => $management->id,
                        'category_id' => $product['category_id'],
                        'product_id' => $product['product_id'],
                        'registration_fee' => $product['registration_fee'],
                        'subscription_fee' => $product['subscription_fee'],
                        'discount_rate' => 0,
                        'created_by' => auth()->id(),
                    ]);
                }

                // 총 개발비 업데이트
                $totalRegistrationFee = SalesContractProduct::where('management_id', $management->id)
                    ->sum('registration_fee');
                $management->update(['total_registration_fee' => $totalRegistrationFee]);
            });

            return response()->json([
                'success' => true,
                'message' => '계약 상품이 저장되었습니다.',
                'management_id' => $managementId,
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '저장 중 오류가 발생했습니다: '.$e->getMessage(),
            ], 500);
        }
    }

    /**
     * 계약 상품 조회
     */
    public function getProducts(int $tenantId): JsonResponse
    {
        $products = SalesContractProduct::where('tenant_id', $tenantId)
            ->with(['product', 'category'])
            ->get();

        $totals = [
            'development_fee' => $products->sum('development_fee'),
            'subscription_fee' => $products->sum('subscription_fee'),
            'count' => $products->count(),
        ];

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