<?php

namespace App\Http\Controllers\Api\V1\ItemMaster;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemMaster\LinkEntityRequest;
use App\Http\Requests\ItemMaster\ReorderRelationshipsRequest;
use App\Services\ItemMaster\EntityRelationshipService;

/**
 * EntityRelationshipController - 엔티티 간 관계(링크) 관리 API
 */
class EntityRelationshipController extends Controller
{
    public function __construct(private EntityRelationshipService $service) {}

    /**
     * 페이지에 섹션 연결
     *
     * POST /api/v1/item-master/pages/{pageId}/link-section
     */
    public function linkSectionToPage(int $pageId, LinkEntityRequest $request)
    {
        return ApiResponse::handle(function () use ($pageId, $request) {
            $data = $request->validated();

            return $this->service->linkSectionToPage(
                $pageId,
                $data['child_id'],
                $data['order_no'] ?? 0
            );
        }, __('message.linked'));
    }

    /**
     * 페이지에서 섹션 연결 해제
     *
     * DELETE /api/v1/item-master/pages/{pageId}/unlink-section/{sectionId}
     */
    public function unlinkSectionFromPage(int $pageId, int $sectionId)
    {
        return ApiResponse::handle(function () use ($pageId, $sectionId) {
            $this->service->unlinkSectionFromPage($pageId, $sectionId);

            return 'success';
        }, __('message.unlinked'));
    }

    /**
     * 페이지에 필드 직접 연결
     *
     * POST /api/v1/item-master/pages/{pageId}/link-field
     */
    public function linkFieldToPage(int $pageId, LinkEntityRequest $request)
    {
        return ApiResponse::handle(function () use ($pageId, $request) {
            $data = $request->validated();

            return $this->service->linkFieldToPage(
                $pageId,
                $data['child_id'],
                $data['order_no'] ?? 0
            );
        }, __('message.linked'));
    }

    /**
     * 페이지에서 필드 연결 해제
     *
     * DELETE /api/v1/item-master/pages/{pageId}/unlink-field/{fieldId}
     */
    public function unlinkFieldFromPage(int $pageId, int $fieldId)
    {
        return ApiResponse::handle(function () use ($pageId, $fieldId) {
            $this->service->unlinkFieldFromPage($pageId, $fieldId);

            return 'success';
        }, __('message.unlinked'));
    }

    /**
     * 섹션에 필드 연결
     *
     * POST /api/v1/item-master/sections/{sectionId}/link-field
     */
    public function linkFieldToSection(int $sectionId, LinkEntityRequest $request)
    {
        return ApiResponse::handle(function () use ($sectionId, $request) {
            $data = $request->validated();

            return $this->service->linkFieldToSection(
                $sectionId,
                $data['child_id'],
                $data['order_no'] ?? 0
            );
        }, __('message.linked'));
    }

    /**
     * 섹션에서 필드 연결 해제
     *
     * DELETE /api/v1/item-master/sections/{sectionId}/unlink-field/{fieldId}
     */
    public function unlinkFieldFromSection(int $sectionId, int $fieldId)
    {
        return ApiResponse::handle(function () use ($sectionId, $fieldId) {
            $this->service->unlinkFieldFromSection($sectionId, $fieldId);

            return 'success';
        }, __('message.unlinked'));
    }

    /**
     * 섹션에 BOM 항목 연결
     *
     * POST /api/v1/item-master/sections/{sectionId}/link-bom
     */
    public function linkBomToSection(int $sectionId, LinkEntityRequest $request)
    {
        return ApiResponse::handle(function () use ($sectionId, $request) {
            $data = $request->validated();

            return $this->service->linkBomToSection(
                $sectionId,
                $data['child_id'],
                $data['order_no'] ?? 0
            );
        }, __('message.linked'));
    }

    /**
     * 섹션에서 BOM 항목 연결 해제
     *
     * DELETE /api/v1/item-master/sections/{sectionId}/unlink-bom/{bomId}
     */
    public function unlinkBomFromSection(int $sectionId, int $bomId)
    {
        return ApiResponse::handle(function () use ($sectionId, $bomId) {
            $this->service->unlinkBomFromSection($sectionId, $bomId);

            return 'success';
        }, __('message.unlinked'));
    }

    /**
     * 페이지의 모든 관계 조회
     *
     * GET /api/v1/item-master/pages/{pageId}/relationships
     */
    public function getPageRelationships(int $pageId)
    {
        return ApiResponse::handle(function () use ($pageId) {
            return $this->service->getPageRelationships($pageId);
        }, __('message.fetched'));
    }

    /**
     * 페이지 구조 조회 (섹션 + 직접 연결된 필드 + 중첩 구조)
     *
     * GET /api/v1/item-master/pages/{pageId}/structure
     */
    public function getPageStructure(int $pageId)
    {
        return ApiResponse::handle(function () use ($pageId) {
            return $this->service->getPageStructure($pageId);
        }, __('message.fetched'));
    }

    /**
     * 섹션의 자식 관계 조회
     *
     * GET /api/v1/item-master/sections/{sectionId}/relationships
     */
    public function getSectionRelationships(int $sectionId)
    {
        return ApiResponse::handle(function () use ($sectionId) {
            return $this->service->getSectionChildRelationships($sectionId);
        }, __('message.fetched'));
    }

    /**
     * 관계 순서 변경
     *
     * POST /api/v1/item-master/relationships/reorder
     */
    public function reorderRelationships(ReorderRelationshipsRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $request->validated();
            $this->service->reorderRelationships(
                $data['parent_type'],
                $data['parent_id'],
                $data['ordered_items']
            );

            return 'success';
        }, __('message.reordered'));
    }
}
