#nullable enable
using System.Collections.Generic;
using System.Linq;
using Kavita.Common.Extensions;
using Kavita.Models.Entities.Person;

namespace Kavita.Models.Builders;

public class PersonBuilder : IEntityBuilder<Person>
{
    private readonly Person _person;
    public Person Build() => _person;

    public PersonBuilder(string name, string? normalizedName = null)
    {
        _person = new Person()
        {
            Name = name.Trim(),
            NormalizedName = string.IsNullOrEmpty(normalizedName) ? name.ToNormalized() : normalizedName,
            SeriesMetadataPeople = new List<SeriesMetadataPeople>(),
            ChapterPeople = new List<ChapterPeople>()
        };
    }

    /// <summary>
    /// Only call for Unit Tests
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public PersonBuilder WithId(int id)
    {
        _person.Id = id;
        return this;
    }

    public PersonBuilder WithAlias(string alias)
    {
        if (_person.Aliases.Any(a => a.NormalizedAlias.Equals(alias.ToNormalized())))
        {
            return this;
        }

        _person.Aliases.Add(new PersonAliasBuilder(alias).Build());

        return this;
    }



    public PersonBuilder WithSeriesMetadata(SeriesMetadataPeople seriesMetadataPeople)
    {
        _person.SeriesMetadataPeople.Add(seriesMetadataPeople);
        return this;
    }

}
