<?php

// Copyright (C) 2010-2026, the Friendica project
// SPDX-FileCopyrightText: 2010-2026 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace Friendica\Module\Moderation\Blocklist;

use Friendica\App\Arguments;
use Friendica\App\BaseURL;
use Friendica\App\Page;
use Friendica\AppHelper;
use Friendica\Content\Pager;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Database\Database;
use Friendica\Model;
use Friendica\Module\BaseModeration;
use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;

class Contact extends BaseModeration
{
	public function __construct(private readonly Database $database, Page $page, AppHelper $appHelper, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
	{
		parent::__construct($page, $appHelper, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
	}

	protected function post(array $request = [])
	{
		$this->checkModerationAccess();

		self::checkFormSecurityTokenRedirectOnError('/moderation/blocklist/contact', 'moderation_contactblock');

		$search      = trim((string) ($request['search'] ?? ''));
		$redirectUrl = 'moderation/blocklist/contact' . ($search !== '' ? '?search=' . rawurlencode($search) : '');

		$contact_url  = $request['contact_url']          ?? '';
		$block_reason = $request['contact_block_reason'] ?? '';
		$block_purge  = $request['contact_block_purge']  ?? false;
		$contacts     = (array) ($request['contacts'] ?? []);

		if (!empty($request['page_contactblock_block'])) {
			$contact = Model\Contact::getByURL($contact_url, null, ['id', 'nurl']);
			if (empty($contact)) {
				$this->systemMessages->addNotice($this->t('Could not find any contact entry for this URL (%s)', $contact_url));
				$this->baseUrl->redirect($redirectUrl);
			}

			if ($this->baseUrl->isLocalUrl($contact['nurl'])) {
				$this->systemMessages->addNotice($this->t('You can\'t block a local contact, please block the user instead'));
				$this->baseUrl->redirect($redirectUrl);
			}

			Model\Contact::block($contact['id'], $block_reason);

			if ($block_purge) {
				foreach (Model\Contact::selectToArray(['id'], ['nurl' => $contact['nurl']]) as $contact) {
					Worker::add(Worker::PRIORITY_LOW, 'Contact\RemoveContent', $contact['id']);
				}
			}

			$this->systemMessages->addInfo($this->t('The contact has been blocked from the node'));
		}

		if (!empty($request['page_contactblock_unblock'])) {
			foreach ($contacts as $uid) {
				Model\Contact::unblock((int) $uid);
			}
			$this->systemMessages->addInfo($this->tt('%s contact unblocked', '%s contacts unblocked', count($contacts)));
		}

		$this->baseUrl->redirect($redirectUrl);
	}

	protected function content(array $request = []): string
	{
		parent::content();

		$condition = ['uid' => 0, 'blocked' => true];
		$search    = trim((string) ($request['search'] ?? ''));
		$condition = self::buildSearchCondition($condition, $search);

		$total = $this->database->count('contact', $condition);

		$pager = new Pager($this->l10n, $this->args->getQueryString(), 30);

		$contacts = Model\Contact::selectToArray([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);

		$t = Renderer::getMarkupTemplate('moderation/blocklist/contact.tpl');
		return Renderer::replaceMacros($t, [
			// strings //
			'$title'            => $this->t('Moderation'),
			'$page'             => $this->t('Remote Contact Blocklist'),
			'$description'      => $this->t('This page allows you to prevent any message from a remote contact to reach your node.'),
			'$submit'           => $this->t('Block Remote Contact'),
			'$importexport'     => $this->t('Import/Export'),
			'$import_csv'       => $this->t('Import from CSV file'),
			'$export_csv'       => $this->t('Export to CSV file'),
			'$select_all'       => $this->t('select all'),
			'$select_none'      => $this->t('select none'),
			'$block'            => $this->t('Block'),
			'$unblock'          => $this->t('Unblock'),
			'$no_data'          => $this->t('No remote contact is blocked from this node.'),
			'$no_data_filtered' => $this->t('No blocked remote contact matches your search.'),
			'$search_label'     => $this->t('Search'),
			'$search_submit'    => $this->t('Filter'),
			'$search_reset'     => $this->t('Reset'),

			'$h_contacts'  => $this->t('Blocked Remote Contacts'),
			'$h_newblock'  => $this->t('Block New Remote Contact'),
			'$th_contacts' => [$this->t('Photo'), $this->t('Name'), $this->t('Reason')],

			'$form_security_token' => self::getFormSecurityToken('moderation_contactblock'),

			// values //
			'$contacts'       => $contacts,
			'$total_contacts' => $this->tt('%s total blocked contact', '%s total blocked contacts', $total),
			'$paginate'       => $pager->renderFull($total),
			'$search'         => $search,

			'$contacturl'           => ['contact_url', $this->t('Profile URL'), '', $this->t('URL of the remote contact to block.')],
			'$contact_block_purge'  => ['contact_block_purge', $this->t('Also purge contact'), false, $this->t('Removes all content related to this contact from the node. Keeps the contact record. This action cannot be undone.')],
			'$contact_block_reason' => ['contact_block_reason', $this->t('Block Reason')],
		]);
	}

	/**
	 * Adds the contact blocklist search filters to a base condition.
	 */
	private static function buildSearchCondition(array $condition, string $search): array
	{
		if ($search === '') {
			return $condition;
		}

		$searchTerm = '%' . $search . '%';

		return DBA::mergeConditions($condition, [
			'(`addr` LIKE ? OR `url` LIKE ? OR `nurl` LIKE ? OR `name` LIKE ? OR `nick` LIKE ? OR `block_reason` LIKE ?)',
			$searchTerm,
			$searchTerm,
			$searchTerm,
			$searchTerm,
			$searchTerm,
			$searchTerm,
		]);
	}
}
