use strict;
use warnings;
use Test::More;
use File::Spec;

# A. Mock Google::Auth
package Google::Auth;
BEGIN { $INC{'Google/Auth.pm'} = 1; }
sub default {
    my ($class, %args) = @_;
    return bless \%args, 'Google::Auth::MockCredentials';
}
package Google::Auth::MockCredentials;
sub get_token {
    return 'mock-token';
}

# B. Mock Google::gRPC::Client
package Google::gRPC::Client;
BEGIN { $INC{'Google/gRPC/Client.pm'} = 1; }
sub new {
    my $class = shift;
    my $args = ( @_ == 1 && ref($_[0]) eq 'HASH' ) ? $_[0] : { @_ };
    return bless $args, $class;
}
sub call {
    my ($self, $args) = @_;
    if ($self->{mock_call}) {
        return $self->{mock_call}->($args);
    }
    die 'No mock_call handler configured in transport!';
}

# C. Fallback Mocks for External Response Classes
BEGIN {
    for my $pkg (qw( Google::Dataflow::V1beta3::Snapshots::DeleteSnapshotResponse Google::Dataflow::V1beta3::Snapshots::ListSnapshotsResponse Google::Dataflow::V1beta3::Snapshots::Snapshot )) {
        unless ($pkg->can('new')) {
            no strict 'refs';
            *{"${pkg}::new"} = sub { bless {}, $_[0] };
            $INC{join('/', split('::', $pkg)) . '.pm'} = 1;
        }
    }
}

# D. Main test execution
package main;
use Google::Cloud::Dataflow::V1Beta3::SnapshotsV1Beta3Client;

my $client = Google::Cloud::Dataflow::V1Beta3::SnapshotsV1Beta3Client->new( credentials => 'dummy' );
ok($client, 'Instantiated generated client');
isa_ok($client->transport, 'Google::gRPC::Client', 'Client transport');

subtest 'get_snapshot method' => sub {
    $client->transport->{mock_call} = sub {
        my ($args) = @_;
        is($args->{service}, 'google.dataflow.v1beta3.SnapshotsV1Beta3', 'Correct service path');
        is($args->{method}, 'GetSnapshot', 'Correct RPC method');
        isa_ok($args->{request}, 'Google::Dataflow::V1beta3::Snapshots::GetSnapshotRequest', 'Request object');
        
        my $response = 'Google::Dataflow::V1beta3::Snapshots::Snapshot'->new();
        return $response;
    };
    
    my $res = $client->get_snapshot();
    ok($res, 'Method returned a response');
    isa_ok($res, 'Google::Dataflow::V1beta3::Snapshots::Snapshot', 'Response object class');
    done_testing();
};

subtest 'delete_snapshot method' => sub {
    $client->transport->{mock_call} = sub {
        my ($args) = @_;
        is($args->{service}, 'google.dataflow.v1beta3.SnapshotsV1Beta3', 'Correct service path');
        is($args->{method}, 'DeleteSnapshot', 'Correct RPC method');
        isa_ok($args->{request}, 'Google::Dataflow::V1beta3::Snapshots::DeleteSnapshotRequest', 'Request object');
        
        my $response = 'Google::Dataflow::V1beta3::Snapshots::DeleteSnapshotResponse'->new();
        return $response;
    };
    
    my $res = $client->delete_snapshot();
    ok($res, 'Method returned a response');
    isa_ok($res, 'Google::Dataflow::V1beta3::Snapshots::DeleteSnapshotResponse', 'Response object class');
    done_testing();
};

subtest 'list_snapshots method' => sub {
    $client->transport->{mock_call} = sub {
        my ($args) = @_;
        is($args->{service}, 'google.dataflow.v1beta3.SnapshotsV1Beta3', 'Correct service path');
        is($args->{method}, 'ListSnapshots', 'Correct RPC method');
        isa_ok($args->{request}, 'Google::Dataflow::V1beta3::Snapshots::ListSnapshotsRequest', 'Request object');
        
        my $response = 'Google::Dataflow::V1beta3::Snapshots::ListSnapshotsResponse'->new();
        return $response;
    };
    
    my $res = $client->list_snapshots();
    ok($res, 'Method returned a response');
    isa_ok($res, 'Google::Dataflow::V1beta3::Snapshots::ListSnapshotsResponse', 'Response object class');
    done_testing();
};

done_testing();
