﻿using GIELIE.Data.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace GIELIE.Data.Entities
{
    public sealed class Ticket : ITicket
    {
        private readonly GielieContext _context;
        public Ticket()
        {
            _context = new GielieContext();
        }

        #region Properties
        [Key]
        public Guid TicketId
        {
            get; set;
        }

        public string Subject
        {
            get; set;
        }

        public Guid AssignedBy
        {
            get;set;
        }

        public Guid AssignedTo
        {
            get; set;
        }

        public DateTime Created
        {
            get;
        } = DateTime.Now;

        public string Description
        {
            get; set;
        }

        public string Notes
        {
            get; set;
        }

        public int Severity
        {
            get; set;
        }

        public int Status
        {
            get; set;
        }
        #endregion

        #region CRUD Operations
        /// <summary>
        /// Save/Update Ticket
        /// </summary>
        /// <returns>Ticket object</returns>
        public Ticket Save()
        {
            try
            {
                if (this != null)
                {
                    var _ticket = _context.Tickets.FirstOrDefault(x => x.TicketId == this.TicketId);
                    if (_ticket != null)
                    {
                        _ticket.Description = this.Description;
                        _ticket.Notes = this.Notes;
                        _ticket.Subject = this.Subject;
                        _ticket.Severity = this.Severity;
                        _ticket.AssignedTo = this.AssignedTo;
                        _ticket.AssignedBy = this.AssignedBy;
                    }
                    else 
                        _context.Tickets.Add(this);

                    _context.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }

            return this;
        }

        /// <summary>
        /// Get all Tickets
        /// </summary>
        /// <returns>list of Toicket objects</returns>
        public List<Ticket> GetTickets()
        {
            try
            {
                return _context.Tickets.ToList();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Get Ticket by TicketId
        /// </summary>
        /// <returns>Ticket object</returns>
        public Ticket GetTicket()
        {
            try
            {
                if (this != null)
                {
                    return _context.Tickets.FirstOrDefault(x => x.TicketId == this.TicketId);
                }
            }
            catch (Exception)
            {
                throw;
            }

            return this;
        }

        /// <summary>
        /// Delete Ticket
        /// </summary>
        public void Delete()
        {
            try
            {
                if (this != null)
                {
                    _context.Tickets.Remove(this);
                    _context.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
    }
}
