using PowderCoating.Application.DTOs.Company;

namespace PowderCoating.Application.Interfaces;

/// <summary>
/// Manages the community formula library: sharing, unsharing, importing, and browsing.
/// </summary>
public interface IFormulaLibraryService
{
    /// <summary>
    /// Returns all published library entries, with AlreadyImported populated for the given company.
    /// Optionally filters by search term, output mode, or industry hint.
    /// </summary>
    Task<IEnumerable<FormulaLibraryCardDto>> BrowseAsync(
        int companyId,
        string? search = null,
        string? outputMode = null,
        string? industryHint = null);

    /// <summary>Full detail for the import preview modal, including field list and formula.</summary>
    Task<FormulaLibraryDetailDto?> GetDetailAsync(int libraryItemId, int companyId);

    /// <summary>
    /// Publishes a company template to the community library.
    /// If the template was previously shared and unpublished, re-publishes the existing row.
    /// Updates the library entry fields from the current template state on re-share.
    /// </summary>
    Task<int> ShareAsync(int companyId, string userId, ShareFormulaRequest request);

    /// <summary>Sets IsPublished = false. Existing imports are unaffected.</summary>
    Task UnshareAsync(int libraryItemId, int companyId);

    /// <summary>
    /// Copies a library entry into the company's local CustomItemTemplate table.
    /// If the company already has an import record for this entry, returns the existing template id.
    /// </summary>
    Task<int> ImportAsync(int libraryItemId, int companyId, string userId);

    /// <summary>
    /// Returns the library status for a given CustomItemTemplate — whether it is shared,
    /// eligible to be shared, and where it was imported from if applicable.
    /// </summary>
    Task<FormulaLibraryStatusDto> GetTemplateLibraryStatusAsync(int templateId, int companyId);

    /// <summary>
    /// Nulls out DiagramImagePath on the FormulaLibraryItem and all imported copies
    /// when a source template's diagram is removed. Call from CompanySettingsController
    /// when a diagram is deleted or replaced.
    /// </summary>
    Task CascadeRemoveDiagramAsync(int sourceCustomItemTemplateId);

    /// <summary>
    /// Records or toggles a thumbs-up/down vote from the given company.
    /// If the same vote already exists it is removed (toggle off).
    /// If the opposite vote exists it is replaced.
    /// Companies cannot rate their own formulas.
    /// Returns the updated counts for the library entry.
    /// </summary>
    Task<(int ThumbsUp, int ThumbsDown, bool? MyVote)> RateAsync(
        int libraryItemId, int companyId, bool isPositive);
}
