package entity

import (
	"git.gocasts.ir/ebhomengo/niki/types"
	"time"
)

type CampaignStatus string

const (
	CampaignDraft    CampaignStatus = "draft"
	CampaignActive   CampaignStatus = "active"
	CampaignFinished CampaignStatus = "completed"
	CampaignPaused   CampaignStatus = "paused"
	CampaignCanceled CampaignStatus = "cancelled"
)

type Campaign struct {
	ID           types.ID       `json:"id"`
	Title        string         `json:"title"`
	Description  string         `json:"description"`
	GoalAmount   float64        `json:"goal_amount"`
	RaisedAmount float64        `json:"raised_amount"`
	Status       CampaignStatus `json:"status"`
	CreatedAt    time.Time      `json:"created_at"`
	DeadlineAt   *time.Time     `json:"deadline_at,omitempty"`
	AdminID      types.ID       `json:"creator_id"`
}

// Behavior
func (c *Campaign) Activate() {
	if c.Status == CampaignDraft {
		c.Status = CampaignActive
	}
}

func (c *Campaign) AddFunds(amount float64) {
	c.RaisedAmount += amount
	if c.RaisedAmount >= c.GoalAmount {
		c.Status = CampaignFinished
	}
}

func (c *Campaign) IsExpired(now time.Time) bool {
	if c.DeadlineAt == nil {
		return false
	}
	return now.After(*c.DeadlineAt)
}
