using BimAI.Application.DTOModels;
using BimAI.Application.DTOModels.Common;
using BimAI.Infrastructure.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace BimAI.API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class InvoiceController(BimAIDbContext context) : ControllerBase
{
    private readonly BimAIDbContext _context = context;

    [HttpGet]
    public async Task<ActionResult<PagedResult<InvoiceDto>>> GetInvoices([FromQuery] InvoiceFilterRequest request)
    {
        var query = _context.Invoices.AsQueryable();

        if (!string.IsNullOrWhiteSpace(request.Search))
        {
            var searchTerm = request.Search.ToLower();
            query = query.Where(x =>
                x.DocumentNo.ToLower().Contains(searchTerm) ||
                x.ClientName.ToLower().Contains(searchTerm) ||
                (x.ClientNip != null && x.ClientNip.ToLower().Contains(searchTerm))
            );
        }

        if (!string.IsNullOrWhiteSpace(request.DocumentNo))
        {
            query = query.Where(x => x.DocumentNo.ToLower().Contains(request.DocumentNo.ToLower()));
        }

        if (!string.IsNullOrWhiteSpace(request.ClientName))
        {
            query = query.Where(x => x.ClientName.ToLower().Contains(request.ClientName.ToLower()));
        }

        if (!string.IsNullOrWhiteSpace(request.Type))
        {
            query = query.Where(x => x.Type.ToLower().Contains(request.Type.ToLower()));
        }

        if (!string.IsNullOrWhiteSpace(request.Source))
        {
            query = query.Where(x => x.Source.ToLower().Contains(request.Source.ToLower()));
        }

        if (request.RegisterDateFrom.HasValue)
        {
            query = query.Where(x => x.RegisterDate >= request.RegisterDateFrom.Value);
        }

        if (request.RegisterDateTo.HasValue)
        {
            query = query.Where(x => x.RegisterDate <= request.RegisterDateTo.Value);
        }

        var totalCount = await query.CountAsync();

        var items = await query
            .OrderByDescending(x => x.RegisterDate)
            .Skip((request.Page - 1) * request.PageSize)
            .Take(request.PageSize)
            .Select(x => new InvoiceDto
            {
                Id = x.Id,
                DocumentNo = x.DocumentNo,
                Type = x.Type,
                RegisterDate = x.RegisterDate,
                SellDate = x.SellDate,
                ClientName = x.ClientName,
                ClientId = x.ClientId,
                ClientNip = x.ClientNip,
                ClientAddress = x.ClientAddress,
                Currency = x.Currency,
                TotalNetto = x.TotalNetto,
                TotalBrutto = x.TotalBrutto,
                TotalVat = x.TotalVat,
                Source = x.Source,
                CreatedAt = x.CreatedAt,
                UpdatedAt = x.UpdatedAt
            })
            .ToListAsync();

        return Ok(new PagedResult<InvoiceDto>
        {
            Items = items,
            TotalCount = totalCount,
            Page = request.Page,
            PageSize = request.PageSize,
        });
    }
}
