package provider

import (
	"context"
	"strings"
	"time"

	"git.toki-labs.com/toki/gito/services/core/internal/core"
)

type ProviderID string

const (
	ProviderGitHub  ProviderID = "github"
	ProviderGitLab  ProviderID = "gitlab"
	ProviderForgejo ProviderID = "forgejo"
	ProviderPlane   ProviderID = "plane"
	ProviderJira    ProviderID = "jira"
)

type Capability uint64

const (
	CapabilityChangeRequest Capability = 1 << iota
	CapabilityWebhook
	CapabilityChecks
	CapabilityComment
)

func (c Capability) Has(flag Capability) bool {
	return flag != 0 && c&flag == flag
}

type Config struct {
	ID            ProviderID
	Endpoint      string
	CredentialRef string
	Capabilities  Capability
	CreatedAt     time.Time
	UpdatedAt     time.Time
}

func (c Config) Supports(flag Capability) bool {
	return c.Capabilities.Has(flag)
}

func (c Config) Normalized() Config {
	c.ID = ProviderID(strings.ToLower(strings.TrimSpace(string(c.ID))))
	c.Endpoint = strings.TrimSpace(c.Endpoint)
	c.CredentialRef = strings.TrimSpace(c.CredentialRef)
	return c
}

type ChangeRequestAdapter interface {
	CreateChangeRequest(ctx context.Context, input core.ChangeRequest) (core.ChangeRequest, error)
	UpdateChangeRequest(ctx context.Context, input core.ChangeRequest) (core.ChangeRequest, error)
	CommentChangeRequest(ctx context.Context, id string, body string) error
	CloseChangeRequest(ctx context.Context, id string) error
}

type WebhookEvent struct {
	Provider   string
	EventType  string
	ExternalID string
	Payload    []byte
}
