using System.Net;
using System.Net.Mime;
using Iceshrimp.Backend.Controllers.Shared.Attributes;
using Iceshrimp.Backend.Controllers.Shared.Schemas;
using Iceshrimp.Backend.Controllers.Web.Renderers;
using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.Backend.Core.Services;
using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;

namespace Iceshrimp.Backend.Controllers.Web;

[ApiController]
[Authenticate]
[Authorize]
[LinkPagination(20, 80)]
[EnableRateLimiting("sliding")]
[Route("/api/iceshrimp/timelines")]
[Produces(MediaTypeNames.Application.Json)]
public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer, CacheService cache) : ControllerBase
{
	[HttpGet("home")]
	[ProducesResults(HttpStatusCode.OK)]
	public async Task<IEnumerable<NoteResponse>> GetHomeTimeline(PaginationQuery pq)
	{
		var user      = HttpContext.GetUserOrFail();
		var heuristic = await QueryableTimelineExtensions.GetHeuristicAsync(user, db, cache);
		var notes = await db.Notes.IncludeCommonProperties()
		                    .FilterByFollowingAndOwn(user, db, heuristic)
		                    .EnsureVisibleFor(user)
		                    .FilterHidden(user, db, filterHiddenListMembers: true)
		                    .FilterMutedThreads(user, db)
		                    .Paginate(pq, ControllerContext)
		                    .PrecomputeVisibilities(user)
		                    .ToListAsync();

		return await noteRenderer.RenderManyAsync(notes.EnforceRenoteReplyVisibility(), user,
		                                          Filter.FilterContext.Home);
	}

	[HttpGet("local")]
	[ProducesResults(HttpStatusCode.OK)]
	public async Task<IEnumerable<NoteResponse>> GetLocalTimeline(PaginationQuery pq)
	{
		var user = HttpContext.GetUserOrFail();
		var notes = await db.Notes.IncludeCommonProperties()
		                    .Where(p => p.UserHost == null)
		                    .EnsureVisibleFor(user)
		                    .FilterHidden(user, db)
		                    .FilterMutedThreads(user, db)
		                    .Paginate(pq, ControllerContext)
		                    .PrecomputeVisibilities(user)
		                    .ToListAsync();

		return await noteRenderer.RenderManyAsync(notes.EnforceRenoteReplyVisibility(), user,
		                                          Filter.FilterContext.Public);
	}

	[HttpGet("social")]
	[ProducesResults(HttpStatusCode.OK)]
	public async Task<IEnumerable<NoteResponse>> GetSocialTimeline(PaginationQuery pq)
	{
		var user      = HttpContext.GetUserOrFail();
		var heuristic = await QueryableTimelineExtensions.GetHeuristicAsync(user, db, cache);
		var notes = await db.Notes.IncludeCommonProperties()
		                    .FilterByFollowingOwnAndLocal(user, db, heuristic)
		                    .EnsureVisibleFor(user)
		                    .FilterHidden(user, db, filterHiddenListMembers: true)
		                    .FilterMutedThreads(user, db)
		                    .Paginate(pq, ControllerContext)
		                    .PrecomputeVisibilities(user)
		                    .ToListAsync();

		return await noteRenderer.RenderManyAsync(notes.EnforceRenoteReplyVisibility(), user,
		                                          Filter.FilterContext.Public);
	}

	[HttpGet("bubble")]
	[ProducesResults(HttpStatusCode.OK)]
	public async Task<IEnumerable<NoteResponse>> GetBubbleTimeline(PaginationQuery pq)
	{
		var user  = HttpContext.GetUserOrFail();
		var notes = await db.Notes.IncludeCommonProperties()
		                    .Where(p => db.BubbleInstances.Any(i => i.Host == p.UserHost))
		                    .EnsureVisibleFor(user)
		                    .FilterHidden(user, db, filterHiddenListMembers: true)
		                    .FilterMutedThreads(user, db)
		                    .Paginate(pq, ControllerContext)
		                    .PrecomputeVisibilities(user)
		                    .ToListAsync();

		return await noteRenderer.RenderManyAsync(notes.EnforceRenoteReplyVisibility(), user,
		                                          Filter.FilterContext.Public);
	}

	[HttpGet("global")]
	[ProducesResults(HttpStatusCode.OK)]
	public async Task<IEnumerable<NoteResponse>> GetGlobalTimeline(PaginationQuery pq)
	{
		var user = HttpContext.GetUserOrFail();
		var notes = await db.Notes.IncludeCommonProperties()
		                    .EnsureVisibleFor(user)
		                    .FilterHidden(user, db)
		                    .FilterMutedThreads(user, db)
		                    .Paginate(pq, ControllerContext)
		                    .PrecomputeVisibilities(user)
		                    .ToListAsync();

		return await noteRenderer.RenderManyAsync(notes.EnforceRenoteReplyVisibility(), user,
		                                          Filter.FilterContext.Public);
	}

	[HttpGet("remote/{instance}")]
	[ProducesResults(HttpStatusCode.OK)]
	public async Task<IEnumerable<NoteResponse>> GetRemoteTimeline(string instance, PaginationQuery pq)
	{
		var user = HttpContext.GetUserOrFail();
		var notes = await db.Notes.IncludeCommonProperties()
		                    .Where(p => p.UserHost == instance)
		                    .EnsureVisibleFor(user)
		                    .FilterHidden(user, db)
		                    .FilterMutedThreads(user, db)
		                    .Paginate(pq, ControllerContext)
		                    .PrecomputeVisibilities(user)
		                    .ToListAsync();

		return await noteRenderer.RenderManyAsync(notes.EnforceRenoteReplyVisibility(), user,
		                                          Filter.FilterContext.Public);
	}
}