package apitokens

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/jackc/pgx/v5/pgxpool"
)

type ChatSessionPreset string

const (
	ChatSessionMail      ChatSessionPreset = "mail"
	ChatSessionDrive     ChatSessionPreset = "drive"
	ChatSessionContacts  ChatSessionPreset = "contacts"
	ChatSessionDocs      ChatSessionPreset = "docs"
	ChatSessionStandalone ChatSessionPreset = "standalone"
)

type ChatSessionInput struct {
	Preset      ChatSessionPreset
	DrivePath   string
	AllowWrite  bool
	TTL         time.Duration
}

func CreateChatSession(ctx context.Context, db *pgxpool.Pool, externalID, email string, in ChatSessionInput) (CreatedToken, error) {
	if in.TTL <= 0 {
		in.TTL = 8 * time.Hour
	}
	expiresAt := time.Now().UTC().Add(in.TTL)
	perms, mailScope, driveScope := chatSessionGrants(in)
	name := fmt.Sprintf("UltiAI session %s", time.Now().UTC().Format("2006-01-02 15:04"))
	return Create(ctx, db, externalID, name, perms, mailScope, driveScope, AgendaScope{AllCalendars: true}, &expiresAt)
}

func chatSessionGrants(in ChatSessionInput) ([]PermissionGrant, MailScope, DriveScope) {
	mailScope := MailScope{AllAccounts: true}
	driveScope := DriveScope{AllFolders: true}
	if strings.TrimSpace(in.DrivePath) != "" {
		driveScope = DriveScope{
			AllFolders:  false,
			FolderPaths: []string{in.DrivePath},
		}
	}

	switch in.Preset {
	case ChatSessionMail:
		return []PermissionGrant{
			{Resource: "mail.messages", Read: true},
			{Resource: "mail.search", Read: true},
			{Resource: "mail.send", Write: true},
			{Resource: "mail.labels", Read: true, Write: true},
			{Resource: "contacts.read", Read: true},
			{Resource: "contacts.search", Read: true},
			{Resource: "automation.chat", Read: true},
		}, mailScope, driveScope
	case ChatSessionDrive:
		return []PermissionGrant{
			{Resource: "drive.files", Read: true, Write: in.AllowWrite},
			{Resource: "drive.download", Read: true},
			{Resource: "automation.chat", Read: true},
		}, mailScope, driveScope
	case ChatSessionContacts:
		return []PermissionGrant{
			{Resource: "contacts.read", Read: true},
			{Resource: "contacts.search", Read: true},
			{Resource: "contacts.write", Write: true},
			{Resource: "contacts.delete", Write: true},
			{Resource: "mail.search", Read: true},
			{Resource: "automation.chat", Read: true},
		}, mailScope, driveScope
	case ChatSessionDocs:
		return []PermissionGrant{
			{Resource: "drive.files", Read: true, Write: in.AllowWrite},
			{Resource: "drive.download", Read: true},
			{Resource: "automation.chat", Read: true},
		}, mailScope, driveScope
	default:
		return []PermissionGrant{
			{Resource: "mail.messages", Read: true},
			{Resource: "mail.search", Read: true},
			{Resource: "mail.send", Write: true},
			{Resource: "mail.labels", Read: true, Write: true},
			{Resource: "drive.files", Read: true, Write: true},
			{Resource: "drive.download", Read: true},
			{Resource: "contacts.read", Read: true},
			{Resource: "contacts.search", Read: true},
			{Resource: "contacts.write", Write: true},
			{Resource: "contacts.delete", Write: true},
			{Resource: "agenda.calendars", Read: true, Write: true},
			{Resource: "agenda.events", Read: true},
			{Resource: "agenda.events.write", Write: true},
			{Resource: "agenda.events.delete", Write: true},
			{Resource: "agenda.freebusy", Read: true},
			{Resource: "agenda.response", Write: true},
			{Resource: "automation.search", Read: true},
			{Resource: "automation.chat", Read: true},
		}, mailScope, driveScope
	}
}
