package query

import (
	"context"
	"fmt"
	"time"
)

// NewEngine returns the analytics SQL engine for the given cloud provider.
// Only AWS Athena is implemented in P0; other vendors return a NotReady engine.
func NewEngine(cfg Config) (Engine, error) {
	switch normalizeProvider(cfg.Vendor) {
	case "aws":
		return newAthenaEngine(cfg)
	case "gcp":
		return &notReadyEngine{provider: "gcp", engine: "bigquery"}, nil
	case "azure":
		return &notReadyEngine{provider: "azure", engine: "synapse"}, nil
	case "alicloud", "aliyun":
		return &notReadyEngine{provider: "alicloud", engine: "maxcompute"}, nil
	default:
		return nil, fmt.Errorf("unsupported query vendor: %s", cfg.Vendor)
	}
}

type notReadyEngine struct {
	provider string
	engine   string
}

func (e *notReadyEngine) Name() string { return e.engine }

func (e *notReadyEngine) Submit(ctx context.Context, req *EngineSubmitRequest) (*Handle, error) {
	return nil, &NotReadyError{Provider: e.provider, Engine: e.engine}
}

func (e *notReadyEngine) Status(ctx context.Context, handle *Handle) (*ExecutionStatus, error) {
	return nil, &NotReadyError{Provider: e.provider, Engine: e.engine}
}

func (e *notReadyEngine) FetchPage(ctx context.Context, handle *Handle, nextToken string, maxRows int64) (*Page, error) {
	return nil, &NotReadyError{Provider: e.provider, Engine: e.engine}
}

func (e *notReadyEngine) PresignResult(ctx context.Context, handle *Handle, expires time.Duration) (*PresignedResult, error) {
	return nil, &NotReadyError{Provider: e.provider, Engine: e.engine}
}

func (e *notReadyEngine) Cancel(ctx context.Context, handle *Handle) error {
	return &NotReadyError{Provider: e.provider, Engine: e.engine}
}
