package store

import (
	"context"
	"encoding/json"
	"errors"
	"time"

	"qctextbuilder/internal/domain"
)

var ErrNotFound = errors.New("not found")

type TemplateStore interface {
	UpsertTemplates(ctx context.Context, templates []domain.Template) error
	GetTemplateByID(ctx context.Context, id int64) (*domain.Template, error)
	ListTemplates(ctx context.Context) ([]domain.Template, error)
	SetTemplateManifestStatus(ctx context.Context, templateID int64, status string, onboarded bool) error
}

type ManifestStore interface {
	CreateManifest(ctx context.Context, manifest domain.TemplateManifest, fields []domain.TemplateField) error
	GetActiveManifestByTemplateID(ctx context.Context, templateID int64) (*domain.TemplateManifest, error)
	ListFieldsByManifestID(ctx context.Context, manifestID string) ([]domain.TemplateField, error)
	UpdateFields(ctx context.Context, manifestID string, fields []domain.TemplateField) error
}

type BuildStore interface {
	CreateBuild(ctx context.Context, build domain.SiteBuild) error
	GetBuildByID(ctx context.Context, id string) (*domain.SiteBuild, error)
	ListBuildsByStatuses(ctx context.Context, statuses []string, limit int) ([]domain.SiteBuild, error)
	MarkBuildSubmitted(ctx context.Context, buildID string, jobID int64, status string, qcResult json.RawMessage, startedAt time.Time) error
	UpdateBuildFromJob(ctx context.Context, buildID string, status string, siteID *int64, previewURL string, qcResult json.RawMessage, qcError json.RawMessage, finishedAt *time.Time) error
	UpdateBuildEditorURL(ctx context.Context, buildID string, editorURL string, qcResult json.RawMessage) error
}

type SettingsStore interface {
	UpsertSettings(ctx context.Context, settings domain.AppSettings) error
	GetSettings(ctx context.Context) (*domain.AppSettings, error)
}

type DraftStore interface {
	CreateDraft(ctx context.Context, draft domain.BuildDraft) error
	UpdateDraft(ctx context.Context, draft domain.BuildDraft) error
	GetDraftByID(ctx context.Context, id string) (*domain.BuildDraft, error)
	ListDrafts(ctx context.Context, limit int) ([]domain.BuildDraft, error)
}
