import { IsInt, IsOptional, IsPositive, IsString } from "class-validator";
import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { ResponseStatsClient } from '../responses/ResponseStatsClient';
/**
 * Defines the StatsClient entity.
 * StatsClients can be used to access the protected parts of the stats api (top runners, donators and so on).
*/
@Entity()
export class StatsClient {
    /**
     * Autogenerated unique id (primary key).
     */
    @PrimaryGeneratedColumn()
    @IsInt()
    id: number;

    /**
     * The clients's description.
     * Mostly for better UX when traceing back stuff.
     */
    @Column({ nullable: true })
    @IsOptional()
    @IsString()
    description?: string;

    /**
     * The client's api key prefix.
     * This is used identitfy a client by it's api key.
     */
    @Column({ unique: true })
    @IsString()
    prefix: string;

    /**
     * The client's api key hash.
     * The api key can be used to authenticate against the /stats/** routes.
     */
    @Column()
    @IsString()
    key: string;

    /**
     * The client's api key in plain text.
     * This will only be used to display the full key on creation and updates.
     */
    @IsString()
    @IsOptional()
    cleartextkey?: string;

    @Column({ type: 'bigint', nullable: true, readonly: true })
    @IsInt()
    @IsPositive()
    created_at: number;

    @Column({ type: 'bigint', nullable: true })
    @IsInt()
    @IsPositive()
    updated_at: number;

    @BeforeInsert()
    public setCreatedAt() {
        this.created_at = Math.floor(Date.now() / 1000);
        this.updated_at = Math.floor(Date.now() / 1000);
    }

    @BeforeUpdate()
    public setUpdatedAt() {
        this.updated_at = Math.floor(Date.now() / 1000);
    }

    /**
   * Turns this entity into it's response class.
   */
    public toResponse(): ResponseStatsClient {
        return new ResponseStatsClient(this);
    }
}