using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenArchival.DataAccess; // Assuming your DbContext or service is here

[Route("api/[controller]")]
[ApiController]
public class FilesController : ControllerBase
{
    // Inject your database context or a service to find file paths
    private readonly IDbContextFactory<ApplicationDbContext> _context;

    public FilesController(IDbContextFactory<ApplicationDbContext> dbFactory)
    {
        _context = dbFactory;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetFile(int id)
    {
        await using var context = await _context.CreateDbContextAsync();

        FilePathListing fileRecord = await context.ArtifactFilePaths.FindAsync(id);

        if (fileRecord == null)
        {
            return NotFound();
        }

        var physicalPath = fileRecord.Path;

        if (!System.IO.File.Exists(physicalPath))
        {
            return NotFound();
        }

        var fileStream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read);

        return new FileStreamResult(fileStream, GetMimeType(physicalPath));
    }

    [HttpGet("{id}/small")]
    public async Task<IActionResult> GetSmallThumbnail(int id)
    {
        await using var context = await _context.CreateDbContextAsync();
        FilePathListing fileRecord = await context.ArtifactFilePaths.FindAsync(id);

        if (fileRecord == null) return NotFound();

        var path = !string.IsNullOrEmpty(fileRecord.SmallThumbnailPath) && System.IO.File.Exists(fileRecord.SmallThumbnailPath)
            ? fileRecord.SmallThumbnailPath
            : fileRecord.Path;

        if (!System.IO.File.Exists(path)) return NotFound();

        var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        var mimeType = path == fileRecord.SmallThumbnailPath ? "image/webp" : GetMimeType(path);
        
        return new FileStreamResult(fileStream, mimeType);
    }

    [HttpGet("{id}/large")]
    public async Task<IActionResult> GetLargeThumbnail(int id)
    {
        await using var context = await _context.CreateDbContextAsync();
        FilePathListing fileRecord = await context.ArtifactFilePaths.FindAsync(id);

        if (fileRecord == null) return NotFound();

        var path = !string.IsNullOrEmpty(fileRecord.LargeThumbnailPath) && System.IO.File.Exists(fileRecord.LargeThumbnailPath)
            ? fileRecord.LargeThumbnailPath
            : fileRecord.Path;

        if (!System.IO.File.Exists(path)) return NotFound();

        var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        var mimeType = path == fileRecord.LargeThumbnailPath ? "image/webp" : GetMimeType(path);

        return new FileStreamResult(fileStream, mimeType);
    }

    private static string GetMimeType(string filePath)
    {
        var extension = Path.GetExtension(filePath).ToLowerInvariant();
        return extension switch
        {
            ".jpg" => "image/jpeg",
            ".jpeg" => "image/jpeg",
            ".png" => "image/png",
            ".gif" => "image/gif",
            ".txt" => "text/plain",
            ".pdf" => "application/pdf",
            _ => "application/octet-stream", // Default for unknown file types
        };
    }
}