using Microsoft.EntityFrameworkCore;
using PowderCoating.Core.Entities;

namespace PowderCoating.Infrastructure.Services;

public partial class SeedDataService
{
    /// <summary>
    /// Seeds the global set of powder-coat manufacturer URL patterns used by the AI inventory
    /// lookup service to construct direct product-page links (e.g. a Prismatic Powders color
    /// page from a part number + color name).
    /// </summary>
    /// <remarks>
    /// <para>
    /// These records use <c>CompanyId = 0</c> (a sentinel value meaning "platform-wide") rather
    /// than a specific tenant ID, so a single set of patterns is shared across all companies.
    /// The global query filter that restricts queries to the current tenant's <c>CompanyId</c>
    /// does not apply here; <c>IgnoreQueryFilters()</c> is used for the existence check so the
    /// count includes records belonging to any company including the platform-level 0.
    /// </para>
    /// <para>
    /// Idempotency: returns 0 if any patterns already exist (regardless of which manufacturer)
    /// rather than checking per-manufacturer, because the full set is always inserted together
    /// and partial sets should not occur in practice.
    /// </para>
    /// <para>
    /// <c>ProductUrlTemplate</c> is nullable; a null value (e.g. Powder Buy The Pound) tells
    /// the lookup service to fall back to a search URL rather than a direct product page.
    /// </para>
    /// <para>
    /// This method is <c>public</c> (unlike other seed helpers) because it is called from the
    /// Platform Management → Seed Data page independently of company-scoped seed operations.
    /// </para>
    /// </remarks>
    /// <returns>Number of patterns inserted, or 0 if patterns were already present.</returns>
    public async Task<int> SeedManufacturerPatternsAsync()
    {
        // Check if any patterns already exist (global records, CompanyId = 0)
        var existingCount = await _context.Set<ManufacturerLookupPattern>()
            .IgnoreQueryFilters()
            .CountAsync(p => !p.IsDeleted);

        if (existingCount > 0)
        {
            return 0; // Already seeded
        }

        var patterns = new List<ManufacturerLookupPattern>
        {
            new ManufacturerLookupPattern
            {
                ManufacturerName = "Prismatic Powders",
                Domain = "prismaticpowders.com",
                ProductUrlTemplate = "https://www.prismaticpowders.com/shop/powder-coating-colors/{partNumber}/{slug}",
                SlugTransform = "LowerHyphen",
                IsActive = true,
                Notes = "Requires both part number and color name",
                CompanyId = 0,
                CreatedAt = DateTime.UtcNow
            },
            new ManufacturerLookupPattern
            {
                ManufacturerName = "Columbia Coatings",
                Domain = "columbiacoatings.com",
                ProductUrlTemplate = "https://www.columbiacoatings.com/product/{slug}/",
                SlugTransform = "LowerHyphen",
                IsActive = true,
                Notes = "Color name slug only",
                CompanyId = 0,
                CreatedAt = DateTime.UtcNow
            },
            new ManufacturerLookupPattern
            {
                ManufacturerName = "All Powder Paints",
                Domain = "allpowderpaints.com",
                ProductUrlTemplate = "https://www.allpowderpaints.com/powder-coating-colors/{slug}/",
                SlugTransform = "LowerHyphen",
                IsActive = true,
                Notes = "Color name slug only",
                CompanyId = 0,
                CreatedAt = DateTime.UtcNow
            },
            new ManufacturerLookupPattern
            {
                ManufacturerName = "Tiger Drylac",
                Domain = "tiger-coatings.com",
                ProductUrlTemplate = "https://www.tiger-coatings.com/us-en/shop/show/{partNumber}",
                SlugTransform = "LowerHyphen",
                IsActive = true,
                Notes = "Part number required; normalize slashes to hyphens",
                CompanyId = 0,
                CreatedAt = DateTime.UtcNow
            },
            new ManufacturerLookupPattern
            {
                ManufacturerName = "Powder Buy The Pound",
                Domain = "powderbuythepound.com",
                ProductUrlTemplate = null,
                SlugTransform = "LowerHyphen",
                IsActive = true,
                Notes = "Complex slug; domain used for search URL selection only",
                CompanyId = 0,
                CreatedAt = DateTime.UtcNow
            }
        };

        await _context.Set<ManufacturerLookupPattern>().AddRangeAsync(patterns);
        await _context.SaveChangesAsync();

        return patterns.Count;
    }
}
