# Copyright 2020 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package OpenQA::WebAPI::Controller::API::V1::User;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use DateTime::Format::Pg;
use Feature::Compat::Try;

=pod

=head1 NAME

OpenQA::WebAPI::Controller::API::V1::User

=head1 SYNOPSIS

  use OpenQA::WebAPI::Controller::API::V1::User;

=head1 DESCRIPTION

Implements User API.

=head1 METHODS

=over 4

=item delete()

Deletes an existing user.

=back

=cut

sub delete ($self) {
    my $user = $self->schema->resultset('Users')->find($self->param('id'));
    return $self->render(json => {error => 'Not found'}, status => 404) unless $user;
    my $username = $user->username;
    $user->anonymize;
    $self->emit_event('openqa_user_deleted', {username => $username});
    $self->render(json => {result => 1});
}

=over 4

=item delete_self()

Delete the current user's own account (anonymize all data).

=back

=cut

sub delete_self ($self) {
    my $user = $self->current_user;
    return $self->render(json => {error => 'Not found'}, status => 404) unless $user;
    return $self->render(json => {error => 'User already deleted'}, status => 400) if $user->is_deleted;
    my $username = $user->username;
    $user->anonymize;
    $self->emit_event('openqa_user_deleted', {username => $username});
    $self->render(json => {result => 1});
}

=over 4

=item create_api_key()

Create a new API key.

=back

=cut

sub create_api_key ($self) {
    my $user = $self->current_user;
    my $expiration;
    my $validation = $self->validation;
    $validation->optional('expiration', 'seconds_optional')->datetime;
    return $self->render(
        json => {error => 'Date must be in format ' . DateTime::Format::Pg->format_datetime(DateTime->now())},
        status => 400
    ) if $validation->has_error;
    $expiration
      = $validation->is_valid('expiration')
      ? DateTime::Format::Pg->parse_datetime($validation->param('expiration'))
      : DateTime->now->add(years => 1);
    my $apikey = $user->api_keys->create({t_expiration => $expiration});
    $self->render(json => {id => $apikey->id, key => $apikey->key, t_expiration => $apikey->t_expiration});
}

=over 4

=item list_api_keys()

List API keys of the current user.

=back

=cut

sub list_api_keys ($self) {
    my $user = $self->current_user;
    my @keys = map {
        {
            key => $_->key,
            t_expiration => $_->t_expiration,
            t_created => $_->t_created,
            t_updated => $_->t_updated,
        }
    } $user->api_keys->all;
    $self->render(json => {keys => \@keys});
}

=over 4

=item delete_api_key()

Delete a specific API key of the current user.

=back

=cut

sub delete_api_key ($self) {
    my $user = $self->current_user;
    my $key = $user->api_keys->find({key => $self->param('key')});
    return $self->render(json => {error => 'Not found'}, status => 404) unless $key;
    $key->delete;
    $self->render(json => {result => 1});
}

1;
