using System;
using System.Linq;
using BO.AppServer.Data.Entity;
using Quadarax.Foundation.Core.Data;
using Quadarax.Foundation.Core.Data.Domain;
using Quadarax.Foundation.Core.Data.Interface;
using Quadarax.Foundation.Core.Data.Interface.Domain;
using Quadarax.Foundation.Core.Data.Repository;

namespace BO.AppServer.Data.Repository
{
    public class WorkspaceBillingRepo : EntityRepository<long,  WorkspaceBilling>
    {
        public WorkspaceBillingRepo(IUnitOfWork<DataDomain> unitOfWork) : base(unitOfWork)
        {
            //DefaultIncludes.Add("Workspace");
            DefaultIncludes.Add("BillingPlan");
        }

        public IQueryable<WorkspaceBilling> GetByWorkspace(long workspaceId, IPaging paging)
        {
            return Query(paging).Where(x => x.WorkspaceId == workspaceId);
        }

        public WorkspaceBilling GetByCurrentForWorkspace(long workspaceId)
        {
            return Query(new Paging()).Where(x => x.WorkspaceId == workspaceId).OrderByDescending(x=>x.Created).FirstOrDefault();
        }

        public WorkspaceBilling CreateForWorkspace(long workspaceId, BillingPlan bplan)
        {
            var newBp = New();
            newBp.Created = DateTime.Now;
            newBp.BillingPlan = bplan;
            newBp.ValueNumber = bplan.PstinitialCredits;
            newBp.WorkspaceId = workspaceId;
            return newBp;
        }
    }
}
