using OmsorgCore.Application.Services;
using OmsorgCore.Domain.Entities;
using OmsorgCore.Tests.TestDoubles;

namespace OmsorgCore.Tests.Services;

public class FacilityContactServiceTests
{
    [Fact]
    public async Task CreateAsync_PersistsAllFields()
    {
        var repository = new FakeFacilityContactRepository();
        var sut = new FacilityContactService(repository);
        var facilityId = Guid.NewGuid();

        var contact = new FacilityContact
        {
            FacilityId = facilityId,
            Name = "Frau Müller",
            Role = "Pflegedienstleitung",
            Department = "Pflege",
            PhoneNumber = "0123 456789",
            Email = "mueller@pflegeheim-musterstadt.de",
            Notes = "Bevorzugt Kontakt per E-Mail."
        };

        var created = await sut.CreateAsync(contact);

        Assert.Equal("Frau Müller", created.Name);
        Assert.Equal("Pflegedienstleitung", created.Role);
        Assert.Equal(facilityId, created.FacilityId);
    }

    [Fact]
    public async Task GetByFacilityIdAsync_ReturnsOnlyContactsOfThatFacility()
    {
        var repository = new FakeFacilityContactRepository();
        var sut = new FacilityContactService(repository);
        var facilityIdA = Guid.NewGuid();
        var facilityIdB = Guid.NewGuid();

        await sut.CreateAsync(new FacilityContact { FacilityId = facilityIdA, Name = "Kontakt A1" });
        await sut.CreateAsync(new FacilityContact { FacilityId = facilityIdA, Name = "Kontakt A2" });
        await sut.CreateAsync(new FacilityContact { FacilityId = facilityIdB, Name = "Kontakt B1" });

        var contactsForA = await sut.GetByFacilityIdAsync(facilityIdA);

        Assert.Equal(2, contactsForA.Count);
        Assert.All(contactsForA, c => Assert.Equal(facilityIdA, c.FacilityId));
    }

    [Fact]
    public async Task UpdateAsync_UpdatesAllMutableFields_AndReturnsUpdatedContact()
    {
        var repository = new FakeFacilityContactRepository();
        var sut = new FacilityContactService(repository);
        var created = await sut.CreateAsync(new FacilityContact { FacilityId = Guid.NewGuid(), Name = "Frau Müller" });

        var updates = new FacilityContact
        {
            Name = "Frau Müller-Schmidt",
            Role = "Heimleitung",
            Department = "Verwaltung",
            PhoneNumber = "0170 1111111",
            Email = "mueller-schmidt@pflegeheim-musterstadt.de",
            Notes = "Neue Position seit 2026."
        };

        var updated = await sut.UpdateAsync(created.Id, updates);

        Assert.NotNull(updated);
        Assert.Equal("Frau Müller-Schmidt", updated!.Name);
        Assert.Equal("Heimleitung", updated.Role);
        Assert.Equal("Verwaltung", updated.Department);
        Assert.NotNull(updated.UpdatedAt);
    }

    [Fact]
    public async Task UpdateAsync_UnknownId_ReturnsNull()
    {
        var repository = new FakeFacilityContactRepository();
        var sut = new FacilityContactService(repository);

        var updated = await sut.UpdateAsync(Guid.NewGuid(), new FacilityContact { Name = "X" });

        Assert.Null(updated);
    }
}
