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}
}

func (s *Porxie) GetBlob(ctx context.Context, did syntax.DID, cid cid.Cid) (io.ReadCloser, error) {
	url := fmt.Sprintf("%s/%s/%s", s.url, 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
}
