<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Controller;

use OCA\Mitgliederverwaltung\Service\FamilyService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

/**
 * REST API controller for family CRUD operations.
 *
 * Part of Issue #28.
 */
class FamilyController extends ApiController {

    use ApiControllerTrait;

    private FamilyService $familyService;
    private LoggerInterface $logger;

    public function __construct(
        string $appName,
        IRequest $request,
        FamilyService $familyService,
        LoggerInterface $logger
    ) {
        parent::__construct($appName, $request);
        $this->familyService = $familyService;
        $this->logger = $logger;
    }

    #[NoCSRFRequired]
    public function index(): JSONResponse {
        return $this->handleAction(function () {
            $limit = $this->request->getParam('limit');
            $offset = $this->request->getParam('offset');
            $search = $this->request->getParam('search');

            $limit = $limit !== null ? (int)$limit : null;
            $offset = $offset !== null ? (int)$offset : null;

            if ($search !== null && $search !== '') {
                $families = $this->familyService->search($search);
            } else {
                $families = $this->familyService->findAll($limit, $offset);
            }

            return new JSONResponse([
                'data' => $families,
                'total' => $this->familyService->countAll(),
            ]);
        }, 'Familien auflisten');
    }

    #[NoCSRFRequired]
    public function show(int $id): JSONResponse {
        return $this->handleAction(function () use ($id) {
            return new JSONResponse($this->familyService->find($id));
        }, 'Familie');
    }

    public function create(): JSONResponse {
        return $this->handleAction(function () {
            $family = $this->familyService->create($this->getRequestData());
            return new JSONResponse($family, Http::STATUS_CREATED);
        }, 'Familie erstellen');
    }

    public function update(int $id): JSONResponse {
        return $this->handleAction(function () use ($id) {
            return new JSONResponse($this->familyService->update($id, $this->getRequestData()));
        }, 'Familie');
    }

    public function destroy(int $id): JSONResponse {
        return $this->handleAction(function () use ($id) {
            $this->familyService->delete($id);
            return new JSONResponse(['status' => 'deleted']);
        }, 'Familie');
    }

    public function linkMember(int $id, int $memberId): JSONResponse {
        return $this->handleAction(function () use ($id, $memberId) {
            $this->familyService->linkMember($id, $memberId);
            return new JSONResponse(['status' => 'linked'], Http::STATUS_CREATED);
        }, 'Familie/Mitglied');
    }

    public function unlinkMember(int $id, int $memberId): JSONResponse {
        return $this->handleAction(function () use ($id, $memberId) {
            $this->familyService->unlinkMember($id, $memberId);
            return new JSONResponse(['status' => 'unlinked']);
        }, 'Mitglied');
    }
}
