using CashLineERP.Domain.Common;
using CashLineERP.SharedKernel.IslamicFinance;
using System.ComponentModel.DataAnnotations.Schema;

namespace CashLineERP.Domain.IslamicFinance.ProductProfiles;

public sealed class IslamicFinanceProductProfile : Entity
{
    public long Id { get; private set; }
    public string ProductType { get; private set; } = IslamicFinanceProductCatalog.ToCode(IslamicFinanceProductType.Murabaha);
    public string ProductCode { get; private set; } = string.Empty;
    public string ProductName { get; private set; } = string.Empty;
    public bool IsActive { get; private set; }
    public bool RequiresShariahReview { get; private set; }
    public bool AllowsPartnerFunding { get; private set; }
    public bool AllowsCashLineFunding { get; private set; }
    public string StatusCode { get; private set; } = IslamicFinanceWorkflowCatalog.Draft;
    public long? WorkflowInstanceId { get; private set; }
    public DateTime? SubmittedAtUtc { get; private set; }
    public long? SubmittedByUserId { get; private set; }
    public DateTime? ApprovedAtUtc { get; private set; }
    public long? ApprovedByUserId { get; private set; }
    public DateTime? ActivatedAtUtc { get; private set; }
    public long? ActivatedByUserId { get; private set; }
    [NotMapped]
    public IslamicFinanceProductType ProductTypeValue => IslamicFinanceProductCatalog.ParseProductType(ProductType);
    [NotMapped]
    public IslamicFinanceLifecycleState LifecycleState => IslamicFinanceWorkflowCatalog.ParseStatusCode(StatusCode);
    [NotMapped]
    public IslamicFinanceProductFundingProfile FundingProfile => new(
        AllowsPartnerFunding,
        AllowsCashLineFunding);

    public IslamicFinanceProductProfile(
        IslamicFinanceProductType productType,
        string productCode,
        string productName,
        bool requiresShariahReview,
        bool allowsPartnerFunding,
        bool allowsCashLineFunding)
    {
        if (string.IsNullOrWhiteSpace(productCode))
        {
            throw new InvalidOperationException("Islamic Finance product code is required.");
        }

        if (string.IsNullOrWhiteSpace(productName))
        {
            throw new InvalidOperationException("Islamic Finance product name is required.");
        }

        ProductType = IslamicFinanceProductCatalog.ToCode(productType);
        ProductCode = productCode.Trim().ToUpperInvariant();
        ProductName = productName.Trim();
        RequiresShariahReview = requiresShariahReview;
        AllowsPartnerFunding = allowsPartnerFunding;
        AllowsCashLineFunding = allowsCashLineFunding;

        _ = FundingProfile;
    }

    private IslamicFinanceProductProfile()
    {
    }

    public bool SupportsFundingSource(IslamicFinanceFundingSourceMode fundingSourceMode) =>
        FundingProfile.SupportsMode(fundingSourceMode);

    public void Submit(long actorUserId)
    {
        if (!string.Equals(StatusCode, IslamicFinanceWorkflowCatalog.Draft, StringComparison.OrdinalIgnoreCase)
            && !string.Equals(StatusCode, IslamicFinanceWorkflowCatalog.Returned, StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("Only draft or returned Islamic Finance product profiles can be submitted.");
        }

        StatusCode = IslamicFinanceWorkflowCatalog.Submitted;
        SubmittedAtUtc = DateTime.UtcNow;
        SubmittedByUserId = actorUserId;
        ApprovedAtUtc = null;
        ApprovedByUserId = null;
        ActivatedAtUtc = null;
        ActivatedByUserId = null;
        Touch(actorUserId);
    }

    public void Approve(long actorUserId)
    {
        if (string.Equals(StatusCode, IslamicFinanceWorkflowCatalog.Approved, StringComparison.OrdinalIgnoreCase))
        {
            return;
        }

        if (!string.Equals(StatusCode, IslamicFinanceWorkflowCatalog.Submitted, StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("Only submitted Islamic Finance product profiles can be approved.");
        }

        if (SubmittedByUserId == actorUserId)
        {
            throw new InvalidOperationException("Islamic Finance product-profile maker-checker control prohibits self-approval.");
        }

        StatusCode = IslamicFinanceWorkflowCatalog.Approved;
        ApprovedAtUtc = DateTime.UtcNow;
        ApprovedByUserId = actorUserId;
        Touch(actorUserId);
    }

    public void Activate(long actorUserId)
    {
        if (!string.Equals(StatusCode, IslamicFinanceWorkflowCatalog.Approved, StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("Only approved Islamic Finance product profiles can be activated.");
        }

        IsActive = true;
        StatusCode = IslamicFinanceWorkflowCatalog.Active;
        ActivatedAtUtc = DateTime.UtcNow;
        ActivatedByUserId = actorUserId;
        Touch(actorUserId);
    }
}
