using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography;
using WebApp.DTOs;
using WebApp.Entities;

namespace WebApp.Mapping;

public static class EventMapping
{
    public static Event ToEntity(this EventCreateDto ECDto)
    {
        return new Event()
        {
            Title = ECDto.Title,
            Description = ECDto.Description,
            ImageURL = ECDto.ImageURL,
            Location = ECDto.Location,
            EventDate = DateTime.SpecifyKind(ECDto.EventDate!.Value, DateTimeKind.Utc),
            EventSkills = ECDto.EventSkills,
            EventRegistrations = []
        };
    }

    public static Event ToEntity(this EventUpdateDto EUDto, int id)
    {
        return new Event()
        {
            EventId = id,
            Title = EUDto.Title,
            Description = EUDto.Description,
            ImageURL = EUDto.ImageURL,
            Location = EUDto.Location,
            EventDate = DateTime.SpecifyKind(EUDto.EventDate!.Value, DateTimeKind.Utc),
            EventSkills = EUDto.EventSkills
        };
    }

    public static EventSummaryDto ToEventSummaryDto(this Event myEvent)
    {

        List<SkillSummaryDto>      ssdto = [];
        List<EventRegistrationDto> erdto = [];

        if (myEvent.EventSkills != null)
        {
            foreach (EventSkill es in myEvent.EventSkills)
            {
                ssdto.Add(es.ToSkillSummaryDto());
            }
        }

        if (myEvent.EventRegistrations != null)
        {
            foreach (EventRegistration er in myEvent.EventRegistrations)
            {
                erdto.Add(er.ToEventRegistrationDto());
            }
        }

        return new EventSummaryDto {
            EventId            = myEvent.EventId,
            Organisation       = myEvent.Organisation!.Name,
            OrganisationId     = myEvent.OrganisationId,
            Title              = myEvent.Title,
            Description        = myEvent.Description ?? "",
            Location           = myEvent.Location,
            EventDate          = myEvent.EventDate,
            EventSkills        = ssdto,
            EventRegistrations = erdto
        };
    }
    public static EventSummaryNoErDto ToEventSummaryNoErDto(this Event myEvent)
    {

        return new EventSummaryNoErDto(
            myEvent.EventId,
            myEvent.Organisation!.Name,
            myEvent.OrganisationId,
            myEvent.Title,
            myEvent.Description ?? "",
            myEvent.ImageURL,
            myEvent.Location,
            myEvent.EventDate,
            myEvent.EventSkills
        );
    }

    public static EventRegistrationDto ToEventRegistrationDto(this EventRegistration myER)
    {

        return new EventRegistrationDto {
            EventId      = myER.EventId,
            UserId       = myER.UserId,
            UserName     = myER.User!.FirstName + " " + myER.User!.LastName,
            RegisteredAt = myER.RegisteredAt
        };
    }

    public static EventDetailsDto ToEventDetailsDto(this Event myEvent)
    {
        List<SkillSummaryDto>      ssdto = [];
        List<EventRegistrationDto> erdto = [];

        if (myEvent.EventSkills != null)
        {
            foreach (EventSkill es in myEvent.EventSkills)
            {
                ssdto.Add(es.ToSkillSummaryDto());
            }
        }

        if (myEvent.EventRegistrations != null)
        {
            foreach (EventRegistration er in myEvent.EventRegistrations)
            {
                erdto.Add(er.ToEventRegistrationDto());
            }
        }

        return new EventDetailsDto {
            EventId            = myEvent.EventId,
            OrganisationId     = myEvent.OrganisationId,
            OrganisationName   = myEvent.Organisation!.Name,
            Title              = myEvent.Title,
            Description        = myEvent.Description ?? "",
            Location           = myEvent.Location,
            EventDate          = myEvent.EventDate,
            EventSkills        = ssdto,
            EventRegistrations = erdto
        };
    }
}
