<?php

namespace GemBoards\Entities;

use GemBoards\Classes\AbstractEntity;
use GemBoards\Classes\Datetime;

class TaskAttachment extends AbstractEntity {

	protected string $table       = 'gemboards_task_attachments';
	protected string $object_type = 'task_attachment';

	protected array $data = [
		'task_id'    => 0,
		'user_id'    => 0,
		'file_url'   => '',
		'file_name'  => '',
		'file_type'  => '',
		'file_size'  => '',
		'created_at' => '',
	];

	public function get_task_id( string $context = 'view' ): int {
		return (int) $this->get_prop( 'task_id', $context );
	}

	public function get_user_id( string $context = 'view' ): int {
		return (int) $this->get_prop( 'user_id', $context );
	}

	public function get_file_url( string $context = 'view' ) {
		return $this->get_prop( 'file_url', $context );
	}

	public function get_file_name( string $context = 'view' ) {
		return $this->get_prop( 'file_name', $context );
	}

	public function get_file_type( string $context = 'view' ) {
		return $this->get_prop( 'file_type', $context );
	}

	public function get_file_size( string $context = 'view' ) {
		return $this->get_prop( 'file_size', $context );
	}

	/**
	 * @param string $context
	 *
	 * @return ?Datetime
	 */
	public function get_date_created_at( string $context = 'view' ) {
		return $this->get_prop( 'created_at', $context );
	}

	public function set_task_id( int $value ) {
		$this->set_prop( 'task_id', $value );
	}

	public function set_user_id( int $value ) {
		$this->set_prop( 'user_id', $value );
	}

	public function set_file_url( string $value ) {
		$this->set_prop( 'file_url', $value );
	}

	public function set_file_name( ?string $value ) {
		$this->set_prop( 'file_name', $value );
	}

	public function set_file_type( ?string $value ) {
		$this->set_prop( 'file_type', $value );
	}

	public function set_file_size( ?string $value ) {
		$this->set_prop( 'file_size', $value );
	}

	public function create() {
		parent::create();
		do_action( 'gemboards/task/attachment/created', $this->get_id(), $this->get_task_id() );
	}

	public function delete( bool $force_delete = false ): bool {
		$data = $this->get_data();
		if ( parent::delete( $force_delete ) ) {
			do_action( 'gemboards/task/attachment/deleted', $this->get_id(), $this->get_task_id(), $data );
			return true;
		}

		return false;
	}
}
