package domain

import (
	"context"
	"time"

	"github.com/google/uuid"
)

type TestSessionStatus string

const (
	TestSessionStatusDraft     TestSessionStatus = "draft"
	TestSessionStatusScheduled TestSessionStatus = "scheduled"
)

type TestSession struct {
	ID              uuid.UUID         `json:"id"`
	CompanyID       uuid.UUID         `json:"company_id"`
	CreatedBy       uuid.UUID         `json:"created_by"`
	Title           string            `json:"title"`
	Description     string            `json:"description"`
	StartsAt        *time.Time        `json:"starts_at"`
	EndsAt          *time.Time        `json:"ends_at"`
	Status          TestSessionStatus `json:"status"`
	TotalCandidates int               `json:"total_candidates"`
	CreatedAt       time.Time         `json:"created_at"`
	UpdatedAt       time.Time         `json:"updated_at"`
}

type TestSessionItem struct {
	ID             uuid.UUID  `json:"id"`
	TestSessionID  uuid.UUID  `json:"test_session_id"`
	CandidateID    uuid.UUID  `json:"candidate_id"`
	Email          string     `json:"invitation_email"`
	InvitedAt      *time.Time `json:"invited_at"`
	ExpiredAt      *time.Time `json:"expired_at"`
	CreatedAt      time.Time  `json:"created_at"`
	DeliveryStatus string     `json:"delivery_status"` // from email_logs
}

type TestSessionRepository interface {
	Create(ctx context.Context, session *TestSession, items []TestSessionItem) error
	GetByID(ctx context.Context, id uuid.UUID, companyID uuid.UUID) (*TestSession, []TestSessionItem, error)
	List(ctx context.Context, companyID uuid.UUID, limit, offset int) ([]TestSession, int, error)
	UpdateStatus(ctx context.Context, id uuid.UUID, status TestSessionStatus) error
}

type TestSessionUsecase interface {
	CreateSession(ctx context.Context, req *TestSessionCreateRequest) (*TestSessionResponse, error)
	GetSession(ctx context.Context, id uuid.UUID, companyID uuid.UUID) (*TestSessionResponse, error)
	ListSessions(ctx context.Context, companyID uuid.UUID, limit, page int) ([]TestSession, int, error)
}

type TestSessionCreateRequest struct {
	CompanyID        uuid.UUID  `json:"-"`
	CreatedBy        uuid.UUID  `json:"-"`
	RecruiterEmail   string     `json:"recruiter_email"`
	Title            string     `json:"title"`
	Description      string     `json:"description"`
	StartsAt         *time.Time `json:"starts_at"`
	EndsAt           *time.Time `json:"ends_at"`
	Status           string     `json:"status"`
	InvitationEmails string     `json:"invitation_emails"`
}

type TestSessionResponse struct {
	Session         *TestSession `json:"session"`
	TotalCandidates int          `json:"total_candidates"`
	SentEmails      int          `json:"sent_emails_count"`
	FailedEmails    int          `json:"failed_emails_count"`
}
