using Jiaowu.Api.Contracts;
using Jiaowu.Api.Controllers;
using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Domain.Identity;
using Jiaowu.Api.Infrastructure.Auth;
using Jiaowu.Api.Infrastructure.Persistence;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace Jiaowu.Api.Tests;

public sealed class AttendancePaginationTests
{
    [Fact]
    public async Task TasksAndSheets_AreFilteredAndReturnedByPage()
    {
        await using var connection = new SqliteConnection("Data Source=:memory:");
        await connection.OpenAsync();
        await using var db = new AppDbContext(
            new DbContextOptionsBuilder<AppDbContext>().UseSqlite(connection).Options);
        await db.Database.EnsureCreatedAsync();

        var college = new College { Code = "CS", Name = "计算机学院" };
        var course = new Course
        {
            Code = "CS101", Name = "程序设计基础", CollegeId = college.Id,
            Credits = 4, TotalHours = 64, LectureHours = 48, PracticeHours = 16,
            Nature = CourseNature.MajorRequired, AssessmentMethod = AssessmentMethod.Examination
        };
        var term = new AcademicTerm
        {
            Code = "2026-1", Name = "2026—2027 学年第一学期", AcademicYear = "2026-2027",
            Season = TermSeason.Autumn, StartDate = new DateOnly(2026, 9, 1),
            EndDate = new DateOnly(2027, 1, 20)
        };
        var tasks = Enumerable.Range(1, 11).Select(number => new TeachingTask
        {
            TaskNumber = $"2026-1-CS101-{number:00}", Name = $"程序设计教学班 {number}",
            AcademicTermId = term.Id, CourseId = course.Id, Capacity = 60,
            Status = TeachingTaskStatus.Published
        }).ToList();
        var sheets = Enumerable.Range(1, 11).Select(number => new AttendanceSheet
        {
            TeachingTaskId = tasks[0].Id,
            Name = number == 11 ? "期末扫码点名" : $"第 {number} 周点名",
            AttendanceDate = new DateTime(2026, 9, number),
            Status = number == 11 ? AttendanceSheetStatus.Submitted : AttendanceSheetStatus.Draft,
            CheckInMethod = number == 11 ? AttendanceCheckInMethod.QrCode : AttendanceCheckInMethod.Manual
        }).ToList();
        db.AddRange(college, course, term);
        db.AddRange(tasks);
        db.AddRange(sheets);
        await db.SaveChangesAsync();

        var controller = new AttendanceController(db, new AllScope());
        var taskResult = Assert.IsType<OkObjectResult>(await controller.GetMyTasks(
            term.Id, "程序设计", 1, 10, CancellationToken.None));
        var taskPage = Assert.IsType<PagedResult<object>>(taskResult.Value);
        Assert.Equal(11, taskPage.Total);
        Assert.Equal(10, taskPage.Items.Count);

        var sheetResult = Assert.IsType<OkObjectResult>(await controller.GetSheets(
            tasks[0].Id, "期末", AttendanceSheetStatus.Submitted,
            AttendanceCheckInMethod.QrCode, 1, 10, CancellationToken.None));
        var sheetPage = Assert.IsType<PagedResult<object>>(sheetResult.Value);
        Assert.Equal(1, sheetPage.Total);
        Assert.Single(sheetPage.Items);
    }

    private sealed class AllScope : ICurrentUserDataScope
    {
        public CurrentUserScope Current { get; } = new(
            Guid.NewGuid(), "测试管理员", null, DataScope.All,
            new HashSet<string>([SystemRoles.SuperAdmin]));
    }
}
