/*
 * Copyright (C) 2026 Fluxer Contributors
 *
 * This file is part of Fluxer.
 *
 * Fluxer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fluxer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
 */

import {createTestAccount} from '@fluxer/api/src/auth/tests/AuthTestUtils';
import {
	acceptInvite,
	blockUser,
	createChannelInvite,
	createDmChannel,
	createFriendship,
	createGuild,
	getChannel,
	initiateCall,
	pinMessage,
	sendChannelMessage,
} from '@fluxer/api/src/channel/tests/ChannelTestUtils';
import {ensureSessionStarted} from '@fluxer/api/src/message/tests/MessageTestUtils';
import {type ApiTestHarness, createApiTestHarness} from '@fluxer/api/src/test/ApiTestHarness';
import {HTTP_STATUS} from '@fluxer/api/src/test/TestConstants';
import {createBuilder} from '@fluxer/api/src/test/TestRequestBuilder';
import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';

describe('DM Blocking Behaviors', () => {
	let harness: ApiTestHarness;

	beforeAll(async () => {
		harness = await createApiTestHarness();
	});

	beforeEach(async () => {
		await harness.reset();
	});

	afterAll(async () => {
		await harness?.shutdown();
	});

	describe('DM Creation Blocking', () => {
		it('prevents DM creation when the other user has blocked you, even with mutual guild', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			const guild = await createGuild(harness, user1.token, 'Test Community');
			const systemChannel = await getChannel(harness, user1.token, guild.system_channel_id!);
			const invite = await createChannelInvite(harness, user1.token, systemChannel.id);
			await acceptInvite(harness, user2.token, invite.code);

			await blockUser(harness, user1, user2.userId);

			await createBuilder(harness, user2.token)
				.post('/users/@me/channels')
				.body({recipient_id: user1.userId})
				.expect(HTTP_STATUS.BAD_REQUEST)
				.execute();
		});

		it('prevents DM creation with someone you have blocked, even with mutual guild', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			const guild = await createGuild(harness, user1.token, 'Test Community');
			const systemChannel = await getChannel(harness, user1.token, guild.system_channel_id!);
			const invite = await createChannelInvite(harness, user1.token, systemChannel.id);
			await acceptInvite(harness, user2.token, invite.code);

			await blockUser(harness, user1, user2.userId);

			await createBuilder(harness, user1.token)
				.post('/users/@me/channels')
				.body({recipient_id: user2.userId})
				.expect(HTTP_STATUS.BAD_REQUEST)
				.execute();
		});
	});

	describe('Voice Call Blocking', () => {
		it('prevents the user who blocked someone from initiating calls in the DM', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			await createFriendship(harness, user1, user2);

			const channel = await createDmChannel(harness, user1.token, user2.userId);

			await blockUser(harness, user1, user2.userId);

			const {response, json} = await initiateCall(harness, user1.token, channel.id, [user2.userId], 400);

			expect(response.status).toBe(400);
			expect((json as {code: string}).code).toBe('CANNOT_SEND_MESSAGES_TO_USER');
		});

		it('prevents calls in a DM after one user blocks the other', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			await createFriendship(harness, user1, user2);

			const channel = await createDmChannel(harness, user1.token, user2.userId);

			await blockUser(harness, user1, user2.userId);

			const {response, json} = await initiateCall(harness, user2.token, channel.id, [user1.userId], 400);

			expect(response.status).toBe(400);
			expect((json as {code: string}).code).toBe('CANNOT_SEND_MESSAGES_TO_USER');
		});
	});

	describe('Pin Operation Blocking', () => {
		it('prevents the user who blocked someone from pinning messages in the DM', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			await ensureSessionStarted(harness, user1.token);
			await ensureSessionStarted(harness, user2.token);

			await createFriendship(harness, user1, user2);

			const channel = await createDmChannel(harness, user1.token, user2.userId);

			const msg = await sendChannelMessage(harness, user2.token, channel.id, 'message to pin');

			await blockUser(harness, user1, user2.userId);

			const {response, json} = await pinMessage(harness, user1.token, channel.id, msg.id, 400);

			expect(response.status).toBe(400);
			expect((json as {code: string}).code).toBe('CANNOT_SEND_MESSAGES_TO_USER');
		});

		it('prevents pinning messages in a DM after one user blocks the other', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			await ensureSessionStarted(harness, user1.token);
			await ensureSessionStarted(harness, user2.token);

			await createFriendship(harness, user1, user2);

			const channel = await createDmChannel(harness, user1.token, user2.userId);

			const msg = await sendChannelMessage(harness, user1.token, channel.id, 'message to pin');

			await blockUser(harness, user1, user2.userId);

			const {response, json} = await pinMessage(harness, user2.token, channel.id, msg.id, 400);

			expect(response.status).toBe(400);
			expect((json as {code: string}).code).toBe('CANNOT_SEND_MESSAGES_TO_USER');
		});
	});

	describe('Message Blocking', () => {
		it('prevents messages in a DM after one user blocks the other', async () => {
			const user1 = await createTestAccount(harness);
			const user2 = await createTestAccount(harness);

			await createFriendship(harness, user1, user2);

			const channel = await createDmChannel(harness, user1.token, user2.userId);

			await blockUser(harness, user1, user2.userId);

			await createBuilder(harness, user2.token)
				.post(`/channels/${channel.id}/messages`)
				.body({content: 'hello'})
				.expect(HTTP_STATUS.BAD_REQUEST, 'CANNOT_SEND_MESSAGES_TO_USER')
				.execute();
		});
	});
});
