package blobstore

import (
	"context"
	"fmt"
	"io"
	"net/http"

	"github.com/bluesky-social/indigo/atproto/syntax"
	"github.com/ipfs/go-cid"
)

type Porxie struct {
	url string
}

func NewPorxieBlobStore(url string) *Porxie {
	return &Porxie{url}
}

var _ BlobStore = new(Porxie)

func (s *Porxie) GetBlobUrl(ctx context.Context, did syntax.DID, cid cid.Cid) (string, error) {
	return fmt.Sprintf("%s/%s/%s", s.url, did, cid), nil
}

func (s *Porxie) GetBlob(ctx context.Context, did syntax.DID, cid cid.Cid) (io.ReadCloser, error) {
	url, _ := s.GetBlobUrl(ctx, did, cid)
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("unexpected status: %s", resp.Status)
	}

	return resp.Body, nil
}
