#pragma once

#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_surface.h>

#include <string>

// ============================================================================
// GpuTexture — SDL_GPU texture + sampler wrapper
// Handles sprite textures, render targets, and the 1×1 white utility texture.
// ============================================================================
class GpuTexture {
    public:
        GpuTexture() = default;
        ~GpuTexture() = default;

        // Load from resource path (pack or disk) using stb_image.
        bool fromFile(SDL_GPUDevice* device, const std::string& file_path);

        // Upload pixel data from an SDL_Surface to a new GPU texture + sampler.
        // Uses nearest-neighbor filter for sprite pixel-perfect look.
        bool fromSurface(SDL_GPUDevice* device, SDL_Surface* surface, bool nearest = true);

        // Create an offscreen render target (COLOR_TARGET | SAMPLER usage).
        bool createRenderTarget(SDL_GPUDevice* device, int w, int h, SDL_GPUTextureFormat format);

        // Create a 1×1 opaque white texture (used for untextured geometry).
        bool createWhite(SDL_GPUDevice* device);

        // Release GPU resources.
        void destroy(SDL_GPUDevice* device);

        SDL_GPUTexture* texture() const { return texture_; }
        SDL_GPUSampler* sampler() const { return sampler_; }
        int width() const { return width_; }
        int height() const { return height_; }
        bool isValid() const { return texture_ != nullptr; }

    private:
        bool uploadPixels(SDL_GPUDevice* device, const void* pixels, int w, int h, SDL_GPUTextureFormat format);
        bool createSampler(SDL_GPUDevice* device, bool nearest);

        SDL_GPUTexture* texture_ = nullptr;
        SDL_GPUSampler* sampler_ = nullptr;
        int width_ = 0;
        int height_ = 0;
};
