#include "renderer_api/renderer_api.h"

#include <cmath>
#include <limits>

namespace pp::renderer {

namespace {

[[nodiscard]] bool is_empty_c_string(const char* text) noexcept
{
    return text == nullptr || text[0] == '\0';
}

[[nodiscard]] std::size_t bounded_c_string_length(const char* text, std::size_t limit) noexcept
{
    if (text == nullptr) {
        return 0;
    }

    std::size_t length = 0;
    while (length <= limit && text[length] != '\0') {
        ++length;
    }

    return length;
}

[[nodiscard]] pp::foundation::Status validate_shader_stage_source(
    ShaderStageSource source,
    const char* stage_name) noexcept
{
    if (is_empty_c_string(source.entry_point)) {
        return pp::foundation::Status::invalid_argument(stage_name);
    }

    if (source.source == nullptr || source.source_size == 0U) {
        return pp::foundation::Status::invalid_argument("shader source must not be empty");
    }

    if (source.source_size > max_shader_source_bytes) {
        return pp::foundation::Status::out_of_range("shader source exceeds the configured limit");
    }

    return pp::foundation::Status::success();
}

[[nodiscard]] Extent2D mip_level_extent(Extent2D extent, std::uint32_t level) noexcept
{
    auto width = extent.width;
    auto height = extent.height;
    for (std::uint32_t index = 0; index < level; ++index) {
        width = width > 1U ? width / 2U : 1U;
        height = height > 1U ? height / 2U : 1U;
    }

    return Extent2D { .width = width, .height = height };
}

}

std::uint32_t bytes_per_pixel(TextureFormat format) noexcept
{
    switch (format) {
    case TextureFormat::rgba8:
        return 4;
    case TextureFormat::r8:
        return 1;
    case TextureFormat::depth24_stencil8:
        return 4;
    }

    return 0;
}

std::uint32_t max_mip_levels_for_extent(Extent2D extent) noexcept
{
    if (extent.width == 0U || extent.height == 0U) {
        return 0;
    }

    auto dimension = extent.width > extent.height ? extent.width : extent.height;
    std::uint32_t levels = 1;
    while (dimension > 1U && levels < max_texture_mip_levels) {
        dimension /= 2U;
        ++levels;
    }

    return levels;
}

bool has_texture_usage(TextureUsage usage, TextureUsage required) noexcept
{
    const auto usage_bits = static_cast<std::uint32_t>(usage);
    const auto required_bits = static_cast<std::uint32_t>(required);
    return required_bits != 0U && (usage_bits & required_bits) == required_bits;
}

pp::foundation::Status validate_extent(Extent2D extent) noexcept
{
    if (extent.width == 0 || extent.height == 0) {
        return pp::foundation::Status::invalid_argument("texture extent must be greater than zero");
    }

    if (extent.width > max_texture_dimension || extent.height > max_texture_dimension) {
        return pp::foundation::Status::out_of_range("texture extent exceeds the configured limit");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_texture_usage(TextureUsage usage) noexcept
{
    constexpr auto allowed_usage = TextureUsage::sampled
        | TextureUsage::render_target
        | TextureUsage::upload_destination
        | TextureUsage::readback_source
        | TextureUsage::copy_source
        | TextureUsage::copy_destination;

    const auto usage_bits = static_cast<std::uint32_t>(usage);
    const auto allowed_bits = static_cast<std::uint32_t>(allowed_usage);
    if (usage_bits == 0U) {
        return pp::foundation::Status::invalid_argument("texture usage must not be empty");
    }

    if ((usage_bits & ~allowed_bits) != 0U) {
        return pp::foundation::Status::invalid_argument("texture usage contains unsupported flags");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_resource_label(const char* label) noexcept
{
    if (label == nullptr) {
        return pp::foundation::Status::invalid_argument("resource label must not be null");
    }

    if (bounded_c_string_length(label, max_resource_label_bytes) > max_resource_label_bytes) {
        return pp::foundation::Status::out_of_range("resource label exceeds the configured limit");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_texture_desc(TextureDesc desc) noexcept
{
    const auto extent_status = validate_extent(desc.extent);
    if (!extent_status.ok()) {
        return extent_status;
    }

    if (bytes_per_pixel(desc.format) == 0U) {
        return pp::foundation::Status::invalid_argument("texture format is not supported");
    }

    if (desc.mip_levels == 0U) {
        return pp::foundation::Status::invalid_argument("texture mip level count must be greater than zero");
    }

    if (desc.mip_levels > max_mip_levels_for_extent(desc.extent)) {
        return pp::foundation::Status::out_of_range("texture mip level count exceeds the texture extent");
    }

    const auto usage_status = validate_texture_usage(desc.usage);
    if (!usage_status.ok()) {
        return usage_status;
    }

    return validate_resource_label(desc.debug_name);
}

pp::foundation::Result<std::uint64_t> texture_byte_size(TextureDesc desc) noexcept
{
    const auto desc_status = validate_texture_desc(desc);
    if (!desc_status.ok()) {
        return pp::foundation::Result<std::uint64_t>::failure(desc_status);
    }

    const auto bpp = static_cast<std::uint64_t>(bytes_per_pixel(desc.format));
    std::uint64_t bytes = 0;
    for (std::uint32_t level = 0; level < desc.mip_levels; ++level) {
        const auto level_extent = mip_level_extent(desc.extent, level);
        const auto width = static_cast<std::uint64_t>(level_extent.width);
        const auto height = static_cast<std::uint64_t>(level_extent.height);
        if (width > std::numeric_limits<std::uint64_t>::max() / height) {
            return pp::foundation::Result<std::uint64_t>::failure(
                pp::foundation::Status::out_of_range("texture size overflows uint64"));
        }

        const auto pixels = width * height;
        if (pixels > std::numeric_limits<std::uint64_t>::max() / bpp) {
            return pp::foundation::Result<std::uint64_t>::failure(
                pp::foundation::Status::out_of_range("texture byte size overflows uint64"));
        }

        const auto level_bytes = pixels * bpp;
        if (bytes > std::numeric_limits<std::uint64_t>::max() - level_bytes) {
            return pp::foundation::Result<std::uint64_t>::failure(
                pp::foundation::Status::out_of_range("texture byte size overflows uint64"));
        }
        bytes += level_bytes;
    }

    if (bytes > max_texture_bytes) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::out_of_range("texture byte size exceeds the configured limit"));
    }

    return pp::foundation::Result<std::uint64_t>::success(bytes);
}

pp::foundation::Status validate_viewport(Viewport viewport, Extent2D target_extent) noexcept
{
    const auto extent_status = validate_extent(target_extent);
    if (!extent_status.ok()) {
        return extent_status;
    }

    if (viewport.x < 0 || viewport.y < 0) {
        return pp::foundation::Status::invalid_argument("viewport origin must be non-negative");
    }

    if (viewport.width == 0 || viewport.height == 0) {
        return pp::foundation::Status::invalid_argument("viewport size must be greater than zero");
    }

    if (!std::isfinite(viewport.min_depth) || !std::isfinite(viewport.max_depth)) {
        return pp::foundation::Status::invalid_argument("viewport depth range must be finite");
    }

    if (viewport.min_depth < 0.0F || viewport.max_depth > 1.0F || viewport.min_depth > viewport.max_depth) {
        return pp::foundation::Status::out_of_range("viewport depth range must be within 0..1 and ordered");
    }

    const auto x = static_cast<std::uint32_t>(viewport.x);
    const auto y = static_cast<std::uint32_t>(viewport.y);
    if (x > target_extent.width || y > target_extent.height) {
        return pp::foundation::Status::out_of_range("viewport origin is outside the render target");
    }

    if (viewport.width > target_extent.width - x || viewport.height > target_extent.height - y) {
        return pp::foundation::Status::out_of_range("viewport exceeds render target bounds");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_scissor(ScissorRect scissor, Extent2D target_extent) noexcept
{
    const auto extent_status = validate_extent(target_extent);
    if (!extent_status.ok()) {
        return extent_status;
    }

    if (!scissor.enabled) {
        return pp::foundation::Status::success();
    }

    if (scissor.x < 0 || scissor.y < 0) {
        return pp::foundation::Status::invalid_argument("scissor origin must be non-negative");
    }

    if (scissor.width == 0 || scissor.height == 0) {
        return pp::foundation::Status::invalid_argument("scissor size must be greater than zero");
    }

    const auto x = static_cast<std::uint32_t>(scissor.x);
    const auto y = static_cast<std::uint32_t>(scissor.y);
    if (x > target_extent.width || y > target_extent.height) {
        return pp::foundation::Status::out_of_range("scissor origin is outside the render target");
    }

    if (scissor.width > target_extent.width - x || scissor.height > target_extent.height - y) {
        return pp::foundation::Status::out_of_range("scissor exceeds render target bounds");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_render_pass_desc(RenderPassDesc desc) noexcept
{
    if (desc.clear_color_enabled
        && (!std::isfinite(desc.clear_color.r)
            || !std::isfinite(desc.clear_color.g)
            || !std::isfinite(desc.clear_color.b)
            || !std::isfinite(desc.clear_color.a))) {
        return pp::foundation::Status::invalid_argument("render pass clear color must be finite");
    }

    if (desc.clear_depth_enabled && !std::isfinite(desc.clear_depth)) {
        return pp::foundation::Status::invalid_argument("render pass clear depth must be finite");
    }

    if (desc.clear_depth_enabled && (desc.clear_depth < 0.0F || desc.clear_depth > 1.0F)) {
        return pp::foundation::Status::out_of_range("render pass clear depth must be within 0..1");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_blend_factor(BlendFactor factor) noexcept
{
    switch (factor) {
    case BlendFactor::zero:
    case BlendFactor::one:
    case BlendFactor::source_alpha:
    case BlendFactor::one_minus_source_alpha:
    case BlendFactor::destination_alpha:
    case BlendFactor::one_minus_destination_alpha:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("blend factor is not supported");
}

pp::foundation::Status validate_blend_op(BlendOp op) noexcept
{
    switch (op) {
    case BlendOp::add:
    case BlendOp::subtract:
    case BlendOp::reverse_subtract:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("blend operation is not supported");
}

pp::foundation::Status validate_blend_state(BlendState state) noexcept
{
    const auto source_color = validate_blend_factor(state.source_color);
    if (!source_color.ok()) {
        return source_color;
    }

    const auto destination_color = validate_blend_factor(state.destination_color);
    if (!destination_color.ok()) {
        return destination_color;
    }

    const auto color_op = validate_blend_op(state.color_op);
    if (!color_op.ok()) {
        return color_op;
    }

    const auto source_alpha = validate_blend_factor(state.source_alpha);
    if (!source_alpha.ok()) {
        return source_alpha;
    }

    const auto destination_alpha = validate_blend_factor(state.destination_alpha);
    if (!destination_alpha.ok()) {
        return destination_alpha;
    }

    return validate_blend_op(state.alpha_op);
}

pp::foundation::Status validate_compare_op(CompareOp op) noexcept
{
    switch (op) {
    case CompareOp::never:
    case CompareOp::less:
    case CompareOp::equal:
    case CompareOp::less_or_equal:
    case CompareOp::greater:
    case CompareOp::not_equal:
    case CompareOp::greater_or_equal:
    case CompareOp::always:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("depth compare operation is not supported");
}

pp::foundation::Status validate_depth_state(DepthState state) noexcept
{
    return validate_compare_op(state.compare);
}

pp::foundation::Status validate_sampler_filter(SamplerFilter filter) noexcept
{
    switch (filter) {
    case SamplerFilter::nearest:
    case SamplerFilter::linear:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("sampler filter is not supported");
}

pp::foundation::Status validate_sampler_address_mode(SamplerAddressMode mode) noexcept
{
    switch (mode) {
    case SamplerAddressMode::clamp_to_edge:
    case SamplerAddressMode::repeat:
    case SamplerAddressMode::mirrored_repeat:
    case SamplerAddressMode::clamp_to_border:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("sampler address mode is not supported");
}

pp::foundation::Status validate_sampler_desc(SamplerDesc desc) noexcept
{
    const auto min_filter = validate_sampler_filter(desc.min_filter);
    if (!min_filter.ok()) {
        return min_filter;
    }

    const auto mag_filter = validate_sampler_filter(desc.mag_filter);
    if (!mag_filter.ok()) {
        return mag_filter;
    }

    const auto mip_filter = validate_sampler_filter(desc.mip_filter);
    if (!mip_filter.ok()) {
        return mip_filter;
    }

    const auto address_u = validate_sampler_address_mode(desc.address_u);
    if (!address_u.ok()) {
        return address_u;
    }

    const auto address_v = validate_sampler_address_mode(desc.address_v);
    if (!address_v.ok()) {
        return address_v;
    }

    return validate_sampler_address_mode(desc.address_w);
}

pp::foundation::Status validate_mesh_desc(MeshDesc desc) noexcept
{
    const auto label_status = validate_resource_label(desc.debug_name);
    if (!label_status.ok()) {
        return label_status;
    }

    if (desc.vertex_count == 0) {
        return pp::foundation::Status::invalid_argument("mesh must contain at least one vertex");
    }

    if (desc.vertex_count > max_mesh_vertices || desc.index_count > max_mesh_vertices) {
        return pp::foundation::Status::out_of_range("mesh vertex or index count exceeds the configured limit");
    }

    switch (desc.topology) {
    case PrimitiveTopology::triangles:
    case PrimitiveTopology::triangle_strip:
    case PrimitiveTopology::lines:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("mesh topology is not supported");
}

pp::foundation::Status validate_draw_desc(MeshDesc mesh, DrawDesc draw) noexcept
{
    const auto mesh_status = validate_mesh_desc(mesh);
    if (!mesh_status.ok()) {
        return mesh_status;
    }

    if (draw.instance_count == 0) {
        return pp::foundation::Status::invalid_argument("draw instance count must be greater than zero");
    }

    if (draw.vertex_count == 0 && draw.index_count == 0) {
        return pp::foundation::Status::invalid_argument("draw must include vertices or indices");
    }

    if (draw.index_count > 0) {
        if (mesh.index_count == 0) {
            return pp::foundation::Status::invalid_argument("indexed draw requires an indexed mesh");
        }

        if (draw.first_index > mesh.index_count || draw.index_count > mesh.index_count - draw.first_index) {
            return pp::foundation::Status::out_of_range("draw index range exceeds the bound mesh");
        }

        return pp::foundation::Status::success();
    }

    if (draw.first_vertex > mesh.vertex_count || draw.vertex_count > mesh.vertex_count - draw.first_vertex) {
        return pp::foundation::Status::out_of_range("draw vertex range exceeds the bound mesh");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_texture_slot(std::uint32_t slot) noexcept
{
    if (slot >= max_texture_slots) {
        return pp::foundation::Status::out_of_range("texture slot exceeds the configured limit");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_shader_program_desc(ShaderProgramDesc desc) noexcept
{
    const auto label_status = validate_resource_label(desc.debug_name);
    if (!label_status.ok()) {
        return label_status;
    }

    const auto vertex_status = validate_shader_stage_source(
        desc.vertex,
        "vertex shader entry point must not be empty");
    if (!vertex_status.ok()) {
        return vertex_status;
    }

    const auto fragment_status = validate_shader_stage_source(
        desc.fragment,
        "fragment shader entry point must not be empty");
    if (!fragment_status.ok()) {
        return fragment_status;
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_shader_uniform_write(
    const char* name,
    std::span<const std::byte> bytes) noexcept
{
    if (is_empty_c_string(name)) {
        return pp::foundation::Status::invalid_argument("shader uniform name must not be empty");
    }

    if (bytes.empty()) {
        return pp::foundation::Status::invalid_argument("shader uniform bytes must not be empty");
    }

    if (bytes.size() > max_shader_uniform_bytes) {
        return pp::foundation::Status::out_of_range("shader uniform bytes exceed the configured limit");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_trace_label(const char* component, const char* name) noexcept
{
    if (is_empty_c_string(component)) {
        return pp::foundation::Status::invalid_argument("trace component must not be empty");
    }

    if (is_empty_c_string(name)) {
        return pp::foundation::Status::invalid_argument("trace name must not be empty");
    }

    if (bounded_c_string_length(component, max_trace_label_bytes) > max_trace_label_bytes) {
        return pp::foundation::Status::out_of_range("trace component exceeds the configured limit");
    }

    if (bounded_c_string_length(name, max_trace_label_bytes) > max_trace_label_bytes) {
        return pp::foundation::Status::out_of_range("trace name exceeds the configured limit");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_readback_region(TextureDesc desc, ReadbackRegion region) noexcept
{
    const auto desc_status = validate_texture_desc(desc);
    if (!desc_status.ok()) {
        return desc_status;
    }

    if (region.width == 0 || region.height == 0) {
        return pp::foundation::Status::invalid_argument("readback region must be greater than zero");
    }

    if (region.x > desc.extent.width || region.y > desc.extent.height) {
        return pp::foundation::Status::out_of_range("readback origin is outside the texture");
    }

    if (region.width > desc.extent.width - region.x || region.height > desc.extent.height - region.y) {
        return pp::foundation::Status::out_of_range("readback region exceeds texture bounds");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Result<std::uint64_t> readback_byte_size(TextureDesc desc, ReadbackRegion region) noexcept
{
    const auto region_status = validate_readback_region(desc, region);
    if (!region_status.ok()) {
        return pp::foundation::Result<std::uint64_t>::failure(region_status);
    }

    const auto bpp = static_cast<std::uint64_t>(bytes_per_pixel(desc.format));
    if (bpp == 0) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::invalid_argument("texture format is not supported"));
    }

    const auto width = static_cast<std::uint64_t>(region.width);
    const auto height = static_cast<std::uint64_t>(region.height);
    if (width > std::numeric_limits<std::uint64_t>::max() / height) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::out_of_range("readback pixel count overflows uint64"));
    }

    const auto pixels = width * height;
    if (pixels > std::numeric_limits<std::uint64_t>::max() / bpp) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::out_of_range("readback byte size overflows uint64"));
    }

    const auto bytes = pixels * bpp;
    if (bytes > max_texture_bytes) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::out_of_range("readback byte size exceeds the configured limit"));
    }

    return pp::foundation::Result<std::uint64_t>::success(bytes);
}

pp::foundation::Result<std::uint64_t> frame_capture_byte_size(TextureDesc desc) noexcept
{
    const auto desc_status = validate_texture_desc(desc);
    if (!desc_status.ok()) {
        return pp::foundation::Result<std::uint64_t>::failure(desc_status);
    }

    if (!has_texture_usage(desc.usage, TextureUsage::render_target)) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::invalid_argument("frame capture source must be a render target"));
    }

    if (!has_texture_usage(desc.usage, TextureUsage::readback_source)) {
        return pp::foundation::Result<std::uint64_t>::failure(
            pp::foundation::Status::invalid_argument("frame capture source must allow readback"));
    }

    return texture_byte_size(desc);
}

pp::foundation::Status validate_texture_copy_descs(
    TextureDesc source,
    ReadbackRegion source_region,
    TextureDesc destination,
    ReadbackRegion destination_region) noexcept
{
    if (!has_texture_usage(source.usage, TextureUsage::copy_source)) {
        return pp::foundation::Status::invalid_argument("texture copy source must allow copy_source usage");
    }

    if (!has_texture_usage(destination.usage, TextureUsage::copy_destination)) {
        return pp::foundation::Status::invalid_argument("texture copy destination must allow copy_destination usage");
    }

    if (source.format != destination.format) {
        return pp::foundation::Status::invalid_argument("texture copy endpoints must use matching formats");
    }

    if (source_region.width != destination_region.width || source_region.height != destination_region.height) {
        return pp::foundation::Status::invalid_argument("texture copy regions must have matching dimensions");
    }

    const auto source_status = validate_readback_region(source, source_region);
    if (!source_status.ok()) {
        return source_status;
    }

    return validate_readback_region(destination, destination_region);
}

pp::foundation::Status validate_mipmap_generation_desc(TextureDesc desc) noexcept
{
    const auto desc_status = validate_texture_desc(desc);
    if (!desc_status.ok()) {
        return desc_status;
    }

    if (desc.mip_levels <= 1U) {
        return pp::foundation::Status::invalid_argument("mipmap generation requires more than one mip level");
    }

    if (!has_texture_usage(desc.usage, TextureUsage::sampled)) {
        return pp::foundation::Status::invalid_argument("mipmap texture must allow sampled usage");
    }

    if (!has_texture_usage(desc.usage, TextureUsage::copy_source)) {
        return pp::foundation::Status::invalid_argument("mipmap texture must allow copy_source usage");
    }

    if (!has_texture_usage(desc.usage, TextureUsage::copy_destination)) {
        return pp::foundation::Status::invalid_argument("mipmap texture must allow copy_destination usage");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_texture_state(TextureState state) noexcept
{
    switch (state) {
    case TextureState::undefined:
    case TextureState::shader_read:
    case TextureState::render_target:
    case TextureState::upload_destination:
    case TextureState::copy_source:
    case TextureState::copy_destination:
    case TextureState::readback_source:
    case TextureState::present:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("texture state is not supported");
}

pp::foundation::Status validate_texture_transition_desc(
    TextureDesc desc,
    TextureState before,
    TextureState after) noexcept
{
    const auto desc_status = validate_texture_desc(desc);
    if (!desc_status.ok()) {
        return desc_status;
    }

    const auto before_status = validate_texture_state(before);
    if (!before_status.ok()) {
        return before_status;
    }

    const auto after_status = validate_texture_state(after);
    if (!after_status.ok()) {
        return after_status;
    }

    if (before == after) {
        return pp::foundation::Status::invalid_argument("texture transition must change state");
    }

    if (after == TextureState::undefined) {
        return pp::foundation::Status::invalid_argument("texture transition destination must not be undefined");
    }

    if (after == TextureState::shader_read && !has_texture_usage(desc.usage, TextureUsage::sampled)) {
        return pp::foundation::Status::invalid_argument("shader-read transition requires sampled usage");
    }

    if (after == TextureState::render_target && !has_texture_usage(desc.usage, TextureUsage::render_target)) {
        return pp::foundation::Status::invalid_argument("render-target transition requires render_target usage");
    }

    if (after == TextureState::upload_destination && !has_texture_usage(desc.usage, TextureUsage::upload_destination)) {
        return pp::foundation::Status::invalid_argument("upload transition requires upload_destination usage");
    }

    if (after == TextureState::copy_source && !has_texture_usage(desc.usage, TextureUsage::copy_source)) {
        return pp::foundation::Status::invalid_argument("copy-source transition requires copy_source usage");
    }

    if (after == TextureState::copy_destination && !has_texture_usage(desc.usage, TextureUsage::copy_destination)) {
        return pp::foundation::Status::invalid_argument("copy-destination transition requires copy_destination usage");
    }

    if (after == TextureState::readback_source && !has_texture_usage(desc.usage, TextureUsage::readback_source)) {
        return pp::foundation::Status::invalid_argument("readback transition requires readback_source usage");
    }

    return pp::foundation::Status::success();
}

pp::foundation::Status validate_blit_filter(BlitFilter filter) noexcept
{
    switch (filter) {
    case BlitFilter::nearest:
    case BlitFilter::linear:
        return pp::foundation::Status::success();
    }

    return pp::foundation::Status::invalid_argument("blit filter is not supported");
}

pp::foundation::Status validate_blit_descs(TextureDesc source, TextureDesc destination) noexcept
{
    const auto source_status = validate_texture_desc(source);
    if (!source_status.ok()) {
        return source_status;
    }

    const auto destination_status = validate_texture_desc(destination);
    if (!destination_status.ok()) {
        return destination_status;
    }

    if (!has_texture_usage(source.usage, TextureUsage::render_target)
        || !has_texture_usage(destination.usage, TextureUsage::render_target)) {
        return pp::foundation::Status::invalid_argument("blit endpoints must be render targets");
    }

    if (!has_texture_usage(source.usage, TextureUsage::copy_source)) {
        return pp::foundation::Status::invalid_argument("blit source must allow copy_source usage");
    }

    if (!has_texture_usage(destination.usage, TextureUsage::copy_destination)) {
        return pp::foundation::Status::invalid_argument("blit destination must allow copy_destination usage");
    }

    if (source.format != destination.format) {
        return pp::foundation::Status::invalid_argument("blit endpoints must use matching texture formats");
    }

    const auto source_bytes = texture_byte_size(source);
    if (!source_bytes.ok()) {
        return source_bytes.status();
    }

    const auto destination_bytes = texture_byte_size(destination);
    if (!destination_bytes.ok()) {
        return destination_bytes.status();
    }

    return pp::foundation::Status::success();
}

pp::foundation::Result<PaintFeedbackPlan> plan_paint_feedback(
    RenderDeviceFeatures features,
    TextureDesc target_desc,
    bool complex_blend) noexcept
{
    const auto target_status = validate_texture_desc(target_desc);
    if (!target_status.ok()) {
        return pp::foundation::Result<PaintFeedbackPlan>::failure(target_status);
    }

    if (!has_texture_usage(target_desc.usage, TextureUsage::render_target)) {
        return pp::foundation::Result<PaintFeedbackPlan>::failure(
            pp::foundation::Status::invalid_argument("paint feedback target must allow render_target usage"));
    }

    if (target_desc.format == TextureFormat::depth24_stencil8) {
        return pp::foundation::Result<PaintFeedbackPlan>::failure(
            pp::foundation::Status::invalid_argument("paint feedback target must be a color texture"));
    }

    const auto target_bytes = texture_byte_size(target_desc);
    if (!target_bytes.ok()) {
        return pp::foundation::Result<PaintFeedbackPlan>::failure(target_bytes.status());
    }

    PaintFeedbackPlan plan;
    plan.target_desc = target_desc;
    plan.target_bytes = target_bytes.value();
    plan.complex_blend = complex_blend;
    if (!complex_blend) {
        return pp::foundation::Result<PaintFeedbackPlan>::success(plan);
    }

    plan.reads_destination_color = true;
    if (features.framebuffer_fetch) {
        plan.path = PaintFeedbackPath::framebuffer_fetch;
        plan.requires_explicit_transition = features.explicit_texture_transitions;
        return pp::foundation::Result<PaintFeedbackPlan>::success(plan);
    }

    const bool can_ping_pong = has_texture_usage(target_desc.usage, TextureUsage::sampled)
        && has_texture_usage(target_desc.usage, TextureUsage::copy_source)
        && has_texture_usage(target_desc.usage, TextureUsage::copy_destination)
        && (features.texture_copy || features.render_target_blit);
    if (!can_ping_pong) {
        return pp::foundation::Result<PaintFeedbackPlan>::failure(
            pp::foundation::Status::invalid_argument(
                "complex paint feedback requires framebuffer fetch or sampled copy-capable render targets"));
    }

    plan.path = PaintFeedbackPath::ping_pong_textures;
    plan.requires_auxiliary_texture = true;
    plan.requires_texture_copy = features.texture_copy;
    plan.requires_render_target_blit = !features.texture_copy && features.render_target_blit;
    plan.requires_explicit_transition = features.explicit_texture_transitions;
    plan.auxiliary_desc = target_desc;
    return pp::foundation::Result<PaintFeedbackPlan>::success(plan);
}

const char* texture_format_name(TextureFormat format) noexcept
{
    switch (format) {
    case TextureFormat::rgba8:
        return "rgba8";
    case TextureFormat::r8:
        return "r8";
    case TextureFormat::depth24_stencil8:
        return "depth24_stencil8";
    }

    return "unknown";
}

const char* texture_state_name(TextureState state) noexcept
{
    switch (state) {
    case TextureState::undefined:
        return "undefined";
    case TextureState::shader_read:
        return "shader_read";
    case TextureState::render_target:
        return "render_target";
    case TextureState::upload_destination:
        return "upload_destination";
    case TextureState::copy_source:
        return "copy_source";
    case TextureState::copy_destination:
        return "copy_destination";
    case TextureState::readback_source:
        return "readback_source";
    case TextureState::present:
        return "present";
    }

    return "unknown";
}

const char* primitive_topology_name(PrimitiveTopology topology) noexcept
{
    switch (topology) {
    case PrimitiveTopology::triangles:
        return "triangles";
    case PrimitiveTopology::triangle_strip:
        return "triangle_strip";
    case PrimitiveTopology::lines:
        return "lines";
    }

    return "unknown";
}

const char* blit_filter_name(BlitFilter filter) noexcept
{
    switch (filter) {
    case BlitFilter::nearest:
        return "nearest";
    case BlitFilter::linear:
        return "linear";
    }

    return "unknown";
}

const char* paint_feedback_path_name(PaintFeedbackPath path) noexcept
{
    switch (path) {
    case PaintFeedbackPath::none:
        return "none";
    case PaintFeedbackPath::framebuffer_fetch:
        return "framebuffer_fetch";
    case PaintFeedbackPath::ping_pong_textures:
        return "ping_pong_textures";
    }

    return "unknown";
}

const char* blend_factor_name(BlendFactor factor) noexcept
{
    switch (factor) {
    case BlendFactor::zero:
        return "zero";
    case BlendFactor::one:
        return "one";
    case BlendFactor::source_alpha:
        return "source_alpha";
    case BlendFactor::one_minus_source_alpha:
        return "one_minus_source_alpha";
    case BlendFactor::destination_alpha:
        return "destination_alpha";
    case BlendFactor::one_minus_destination_alpha:
        return "one_minus_destination_alpha";
    }

    return "unknown";
}

const char* blend_op_name(BlendOp op) noexcept
{
    switch (op) {
    case BlendOp::add:
        return "add";
    case BlendOp::subtract:
        return "subtract";
    case BlendOp::reverse_subtract:
        return "reverse_subtract";
    }

    return "unknown";
}

const char* compare_op_name(CompareOp op) noexcept
{
    switch (op) {
    case CompareOp::never:
        return "never";
    case CompareOp::less:
        return "less";
    case CompareOp::equal:
        return "equal";
    case CompareOp::less_or_equal:
        return "less_or_equal";
    case CompareOp::greater:
        return "greater";
    case CompareOp::not_equal:
        return "not_equal";
    case CompareOp::greater_or_equal:
        return "greater_or_equal";
    case CompareOp::always:
        return "always";
    }

    return "unknown";
}

const char* sampler_filter_name(SamplerFilter filter) noexcept
{
    switch (filter) {
    case SamplerFilter::nearest:
        return "nearest";
    case SamplerFilter::linear:
        return "linear";
    }

    return "unknown";
}

const char* sampler_address_mode_name(SamplerAddressMode mode) noexcept
{
    switch (mode) {
    case SamplerAddressMode::clamp_to_edge:
        return "clamp_to_edge";
    case SamplerAddressMode::repeat:
        return "repeat";
    case SamplerAddressMode::mirrored_repeat:
        return "mirrored_repeat";
    case SamplerAddressMode::clamp_to_border:
        return "clamp_to_border";
    }

    return "unknown";
}

}
