#include "app_core/brush_package_export.h"
#include "test_harness.h"

#include <string>
#include <string_view>

namespace {

class FakeBrushPackageExportServices final : public pp::app::BrushPackageExportServices {
public:
    void export_brush_package(std::string_view path, const pp::app::BrushPackageExportRequest& request) override
    {
        exports += 1;
        last_path = std::string(path);
        last_request = request;
    }

    int exports = 0;
    std::string last_path;
    pp::app::BrushPackageExportRequest last_request;
};

void validates_path_and_preserves_metadata_edges(pp::tests::Harness& harness)
{
    pp::app::BrushPackageExportRequest request;
    request.author = "Pano Artist";
    request.email = "artist@example.test";
    request.url = "https://example.test/brushes";
    request.description = "Cloud brushes";
    request.export_data = true;
    request.destination_path = "D:/Paint/BrushPreviews";
    request.has_header_image = true;

    PP_EXPECT(harness, pp::app::validate_brush_package_export_request("D:/Paint/clouds.ppbr", request).ok());
    PP_EXPECT(harness, !pp::app::validate_brush_package_export_request("", request).ok());

    request.export_data = false;
    PP_EXPECT(harness, pp::app::validate_brush_package_export_request("D:/Paint/clouds.ppbr", request).ok());

    request.destination_path.clear();
    PP_EXPECT(harness, pp::app::validate_brush_package_export_request("D:/Paint/clouds.ppbr", request).ok());
}

void executor_dispatches_export_request(pp::tests::Harness& harness)
{
    FakeBrushPackageExportServices services;
    pp::app::BrushPackageExportRequest request;
    request.author = "Pano Artist";
    request.email = "artist@example.test";
    request.url = "https://example.test/brushes";
    request.description = "Cloud brushes";
    request.export_data = true;
    request.destination_path = "D:/Paint/BrushPreviews";
    request.has_header_image = true;

    const auto status = pp::app::execute_brush_package_export("D:/Paint/clouds.ppbr", request, services);
    PP_EXPECT(harness, status.ok());
    PP_EXPECT(harness, services.exports == 1);
    PP_EXPECT(harness, services.last_path == "D:/Paint/clouds.ppbr");
    PP_EXPECT(harness, services.last_request.author == "Pano Artist");
    PP_EXPECT(harness, services.last_request.email == "artist@example.test");
    PP_EXPECT(harness, services.last_request.url == "https://example.test/brushes");
    PP_EXPECT(harness, services.last_request.description == "Cloud brushes");
    PP_EXPECT(harness, services.last_request.destination_path == "D:/Paint/BrushPreviews");
    PP_EXPECT(harness, services.last_request.export_data);
    PP_EXPECT(harness, services.last_request.has_header_image);
}

void executor_rejects_malformed_requests_before_dispatch(pp::tests::Harness& harness)
{
    FakeBrushPackageExportServices services;
    pp::app::BrushPackageExportRequest empty_path_request;
    PP_EXPECT(harness, !pp::app::execute_brush_package_export("", empty_path_request, services).ok());
    PP_EXPECT(harness, services.exports == 0);

    pp::app::BrushPackageExportRequest legacy_flexible_request;
    legacy_flexible_request.destination_path = "D:/Paint/BrushPreviews";
    legacy_flexible_request.export_data = false;
    PP_EXPECT(
        harness,
        pp::app::execute_brush_package_export("D:/Paint/clouds.ppbr", legacy_flexible_request, services).ok());
    PP_EXPECT(harness, services.exports == 1);
}

void success_dialog_preserves_legacy_message(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_brush_package_export_success_dialog("D:/Paint/clouds.ppbr");

    PP_EXPECT(harness, plan.title == "Export PPBR");
    PP_EXPECT(harness, plan.message == "Brushes exported to:\nD:/Paint/clouds.ppbr");
    PP_EXPECT(harness, !plan.show_cancel);
}

} // namespace

int main()
{
    pp::tests::Harness harness;
    harness.run("validates path and preserves metadata edges", validates_path_and_preserves_metadata_edges);
    harness.run("executor dispatches export request", executor_dispatches_export_request);
    harness.run("executor rejects malformed requests before dispatch", executor_rejects_malformed_requests_before_dispatch);
    harness.run("success dialog preserves legacy message", success_dialog_preserves_legacy_message);
    return harness.finish();
}
