// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.

// The resource package handles CRUD operations on resources and YAML (de)serialization
package resource

import (
	"context"
	_ "encoding/json"
	_ "fmt"
	_ "gopkg.in/yaml.v3"
	_ "net/url"
	"gitea.rosskeen.house/rosskeen.house/machine"
)

type ResourceSelector func(r *Declaration) bool

type Resource interface {
	Type() string
	StateMachine() machine.Stater
	URI() string
	SetURI(string) error
	ResolveId(context.Context) string
	ResourceLoader
	StateTransformer
	ResourceReader
	ResourceValidator
	Clone() Resource
}

type ResourceValidator interface {
	Validate() error
}

type ResourceCreator interface {
	Create(context.Context) error
}

type ResourceReader interface {
	Read(context.Context) ([]byte, error)
}

type ResourceUpdater interface {
	Update() error
}

type ResourceDeleter interface {
	Delete() error
}

type ResourceDecoder struct {
}

func NewResource(uri string) Resource {
	r, e := ResourceTypes.New(uri)
	if e == nil {
		return r
	}
	return nil
}

func StorageMachine() machine.Stater {
// start_destroy -> absent -> start_create -> present -> start_destroy
	stater := machine.New("absent")
	stater.AddStates("absent", "start_create", "present", "start_delete", "start_read", "start_update")
	stater.AddTransition("create", "absent", "start_create")
	stater.AddTransition("created", "start_create", "present")
	stater.AddTransition("read", "*", "start_read")
	stater.AddTransition("state_read", "start_read", "present")
	stater.AddTransition("update", "*", "start_update")
	stater.AddTransition("updated", "start_update", "present")
	stater.AddTransition("delete", "*", "start_delete")
	stater.AddTransition("deleted", "start_delete", "absent")
	return stater
}

func ProcessMachine() machine.Stater {
	return nil
}
