package store

import (
	"context"
	"time"

	"chat-switchboard/models"
)

// =========================================
// PROJECT STORE (v0.19.0)
// =========================================

type ProjectStore interface {
	// CRUD
	Create(ctx context.Context, p *models.Project) error
	GetByID(ctx context.Context, id string) (*models.Project, error)
	Update(ctx context.Context, id string, patch models.ProjectPatch) error
	Delete(ctx context.Context, id string) error

	// Listing
	ListForUser(ctx context.Context, userID string, teamIDs []string, includeArchived bool) ([]models.Project, error)

	// Channel association (atomic move: removes old project if any)
	AddChannel(ctx context.Context, projectID, channelID string, position int) error
	RemoveChannel(ctx context.Context, projectID, channelID string) error
	ListChannels(ctx context.Context, projectID string) ([]models.ProjectChannel, error)
	ReorderChannels(ctx context.Context, projectID string, channelIDs []string) error

	// KB association
	AddKB(ctx context.Context, projectID, kbID string, autoSearch bool) error
	RemoveKB(ctx context.Context, projectID, kbID string) error
	ListKBs(ctx context.Context, projectID string) ([]models.ProjectKB, error)

	// Note association
	AddNote(ctx context.Context, projectID, noteID string) error
	RemoveNote(ctx context.Context, projectID, noteID string) error
	ListNotes(ctx context.Context, projectID string) ([]models.ProjectNote, error)

	// Access check
	UserCanAccess(ctx context.Context, userID, projectID string, teamIDs []string) (bool, error)

	// Get project ID for a channel (used during note auto-association)
	GetProjectIDForChannel(ctx context.Context, channelID string) (string, error)

	// Get KB IDs bound to a project (used for virtual injection at completion)
	GetKBIDs(ctx context.Context, projectID string) ([]string, error)

	// ── CS5c additions (v0.29.0) ──

	// AdminList returns all projects with counts and owner names (admin only).
	AdminList(ctx context.Context, includeArchived bool) ([]AdminProject, error)
}

// AdminProject is returned by ProjectStore.AdminList.
type AdminProject struct {
	ID           string
	Name         string
	Description  string
	Scope        string
	OwnerID      string
	TeamID       *string
	IsArchived   bool
	CreatedAt    time.Time
	UpdatedAt    time.Time
	ChannelCount int
	KBCount      int
	NoteCount    int
	OwnerName    string
}
