﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClockUpEventsLib.Models
{
    public class EventRecord
    {
        public int? Id { get; set; } = null; // primary key

        [Index]
        [StringLength(10)]
        public string Provider { get; set; } // "atnd"(4), "connpass"(8), "doorkeeper"(10)

        [Index]
        public int IdInProvider { get; set; } // プロバイダ内ID

        [Index]
        public DateTimeOffset StartsAt { get; set; }

        [Index]
        public DateTimeOffset EndsAt { get; set; }

        [Index]
        public DateTimeOffset EventUpdatedAt { get; set; } // イベント更新者による更新日時 (ClockUpEvents 側の更新日時ではない)

        [StringLength(255)]
        public string Title { get; set; }

        [StringLength(255)]
        public string Url { get; set; }

        public string InfoJson { get; set; }

        public override string ToString()
        {
            string ret = $"{Id},{Provider}_{IdInProvider},{StartsAt},{Url},{Title}";
            return ret;
        }

        public void SetEventInfo(EventInfo info)
        {
            this.Provider = info.GetProvider();
            this.IdInProvider = info.GetIdInProvider();
            this.StartsAt = info.StartsAt;
            this.EndsAt = info.EndsAt;
            this.EventUpdatedAt = info.EventUpdatedAt;
            this.Title = info.Title.Substring(0, Math.Min(255, info.Title.Length));
            this.Url = info.Url.Substring(0, Math.Min(255, info.Url.Length));
            this.InfoJson = JsonConvert.SerializeObject(info);
        }
    }
}
