using TF6760ApiTest.Models;
using TF6760ApiTest.Data;
using Microsoft.EntityFrameworkCore;

namespace TF6760ApiTest.Services
{
    public class ProductService : IProductService
    {
        private readonly DataContext _context;

        public ProductService(DataContext context)
        {
            _context = context;
        }

        public async Task<List<Product>> GetProductsAsync()
        {
            return await _context.Products.ToListAsync();
        }

        public async Task<Product?> GetProductByIdAsync(int id)
        {
            return await _context.Products.FindAsync(id);
        }

        public async Task<Product?> GetProductByBarCodeAsync(string barCode)
        {
            return await _context.Products.FirstOrDefaultAsync(p => p.BarCode == barCode);
        }

        public async Task<Product> AddProductAsync(Product product)
        {
            // 判断条码是否重复
            bool exists = await _context.Products.AnyAsync(p => p.BarCode == product.BarCode);
            if (exists)
            {
                throw new InvalidOperationException("条码已存在，不能重复添加。");
            }

            _context.Products.Add(product);
            await _context.SaveChangesAsync();
            return product;
        }

        public async Task<Product> UpdateProductAsync(Product product)
        {
            // 根据 BarCode 查找产品
            var existing = await _context.Products.FirstOrDefaultAsync(p => p.BarCode == product.BarCode);
            if (existing == null)
                return null!;

            // 更新属性
            existing.CreatDateTime = DateTime.Now;
            existing.FinalPressure = product.FinalPressure;
            existing.FinalDisplacement = product.FinalDisplacement;
            existing.IsQualified = product.IsQualified;

            await _context.SaveChangesAsync();
            return existing;
        }

        public async Task DeleteProductAsync(int id)
        {
            var product = await _context.Products.FindAsync(id);
            if (product != null)
            {
                _context.Products.Remove(product);
                await _context.SaveChangesAsync();
            }
        }

        public async Task DeleteProductByBarCodeAsync(string barCode)
        {
            var product = await _context.Products.FirstOrDefaultAsync(p => p.BarCode == barCode);
            if (product != null)
            {
                _context.Products.Remove(product);
                await _context.SaveChangesAsync();
            }
        }

        public async Task<PagedResult<Product>> GetProductsPagedAsync(int pageNumber, int pageSize)
        {
            var query = _context.Products.AsQueryable();
            var totalItems = await query.CountAsync();
            var items = await query
                .OrderBy(p => p.Id)
                .Skip((pageNumber - 1) * pageSize)
                .Take(pageSize)
                .ToListAsync();

            return new PagedResult<Product>(items, totalItems, pageNumber, pageSize);
        }
    }
}
