package funding

import (
	"context"

	"github.com/google/uuid"
)

// Store defines the interface for funding application persistence
type Store interface {
	// Application CRUD
	CreateApplication(ctx context.Context, app *FundingApplication) error
	GetApplication(ctx context.Context, id uuid.UUID) (*FundingApplication, error)
	GetApplicationByNumber(ctx context.Context, number string) (*FundingApplication, error)
	UpdateApplication(ctx context.Context, app *FundingApplication) error
	DeleteApplication(ctx context.Context, id uuid.UUID) error

	// List & Search
	ListApplications(ctx context.Context, tenantID uuid.UUID, filter ApplicationFilter) (*ApplicationListResponse, error)
	SearchApplications(ctx context.Context, tenantID uuid.UUID, query string) ([]FundingApplication, error)

	// Wizard Data
	SaveWizardStep(ctx context.Context, appID uuid.UUID, step int, data map[string]interface{}) error
	GetWizardProgress(ctx context.Context, appID uuid.UUID) (*WizardProgress, error)

	// Attachments
	AddAttachment(ctx context.Context, appID uuid.UUID, attachment *Attachment) error
	GetAttachments(ctx context.Context, appID uuid.UUID) ([]Attachment, error)
	DeleteAttachment(ctx context.Context, attachmentID uuid.UUID) error

	// Application History (Audit Trail)
	AddHistoryEntry(ctx context.Context, entry *ApplicationHistoryEntry) error
	GetHistory(ctx context.Context, appID uuid.UUID) ([]ApplicationHistoryEntry, error)

	// Statistics
	GetStatistics(ctx context.Context, tenantID uuid.UUID) (*FundingStatistics, error)

	// Export Tracking
	SaveExportBundle(ctx context.Context, bundle *ExportBundle) error
	GetExportBundle(ctx context.Context, bundleID uuid.UUID) (*ExportBundle, error)
}

// ApplicationFilter for filtering list queries
type ApplicationFilter struct {
	Status         *ApplicationStatus `json:"status,omitempty"`
	FundingProgram *FundingProgram    `json:"funding_program,omitempty"`
	FederalState   *FederalState      `json:"federal_state,omitempty"`
	CreatedAfter   *string            `json:"created_after,omitempty"`
	CreatedBefore  *string            `json:"created_before,omitempty"`
	Page           int                `json:"page"`
	PageSize       int                `json:"page_size"`
	SortBy         string             `json:"sort_by,omitempty"`
	SortOrder      string             `json:"sort_order,omitempty"` // asc, desc
}

// ApplicationHistoryEntry for audit trail
type ApplicationHistoryEntry struct {
	ID            uuid.UUID              `json:"id"`
	ApplicationID uuid.UUID              `json:"application_id"`
	Action        string                 `json:"action"` // created, updated, submitted, approved, etc.
	ChangedFields []string               `json:"changed_fields,omitempty"`
	OldValues     map[string]interface{} `json:"old_values,omitempty"`
	NewValues     map[string]interface{} `json:"new_values,omitempty"`
	PerformedBy   uuid.UUID              `json:"performed_by"`
	PerformedAt   string                 `json:"performed_at"`
	Notes         string                 `json:"notes,omitempty"`
}

// FundingStatistics for dashboard
type FundingStatistics struct {
	TotalApplications  int     `json:"total_applications"`
	DraftCount         int     `json:"draft_count"`
	SubmittedCount     int     `json:"submitted_count"`
	ApprovedCount      int     `json:"approved_count"`
	RejectedCount      int     `json:"rejected_count"`
	TotalRequested     float64 `json:"total_requested"`
	TotalApproved      float64 `json:"total_approved"`
	AverageProcessDays float64 `json:"average_process_days"`
	ByProgram          map[FundingProgram]int `json:"by_program"`
	ByState            map[FederalState]int   `json:"by_state"`
}
