<?php

namespace App\Models;

use Carbon\Carbon;
use Clarkeash\Doorman\Facades\Doorman;
use Clarkeash\Doorman\Models\Invite;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Leenooks\Traits\ScopeActive;

use App\Interfaces\IDs;
use App\Traits\{NextKey,PushNew};

/**
 * Class Invoice
 * Invoices that belong to an Account
 *
 * Attributes for services:
 * + due                : Balance due on an invoice
 * + due_date           : Date the invoice is due
 * + invoice_date       : Date the invoice was created
 * + lid                : Local ID for invoice
 * + paid               : Total of payments received (excluding pending)
 * + paid_date          : Date the invoice was paid in full
 * + paid_pending       : Total of pending payments received
 * + sid                : System ID for invoice
 * + sub_total          : Invoice sub-total before taxes
 * + total_tax          : Invoices total of taxes
 * + total              : Invoice total
 *
 * @package App\Models
 */
class Invoice extends Model implements IDs
{
	use PushNew,ScopeActive;

	protected $casts = [
		'reminders'=>'json',
	];

	protected $dates = [
		'due_at',
	];

	public const BILL_WEEKLY = 0;
	public const BILL_MONTHLY = 1;
	public const BILL_QUARTERLY = 2;
	public const BILL_SEMI_YEARLY = 3;
	public const BILL_YEARLY = 4;
	public const BILL_TWOYEARS = 5;
	public const BILL_THREEYEARS = 6;
	public const BILL_FOURYEARS = 7;
	public const BILL_FIVEYEARS = 8;

	/* Our available billing periods */
	public const billing_periods = [
		self::BILL_WEEKLY => [
			'name' => 'Weekly',
			'interval' => 0.25,
		],
		self::BILL_MONTHLY => [
			'name' => 'Monthly',
			'interval' => 1,
		],
		self::BILL_QUARTERLY => [
			'name' => 'Quarterly',
			'interval' => 3,
		],
		self::BILL_SEMI_YEARLY => [
			'name' => 'Semi-Annually',
			'interval' => 6,
		],
		self::BILL_YEARLY => [
			'name' => 'Annually',
			'interval' => 12,
		],
		self::BILL_TWOYEARS => [
			'name' => 'Two years',
			'interval' => 24,
		],
		self::BILL_THREEYEARS => [
			'name' => 'Three Years',
			'interval' => 36,
		],
		self::BILL_FOURYEARS => [
			'name' => 'Four Years',
			'interval' => 48,
		],
		SELF::BILL_FIVEYEARS => [
			'name' => 'Five Years',
			'interval' => 60,
		],
	];

	// Array of items that can be updated with PushNew
	protected $pushable = ['items'];

	/*
	protected $with = [
		'account.country.currency',
		'items.taxes',
		'paymentitems'
	];
	*/

	// Caching variables
	private int $_paid = 0;
	private int $_total = 0;
	private int $_total_tax = 0;

	/* STATIC METHODS */

	/**
	 * This works out what multiplier to use to change billing periods
	 *
	 * @param int $source
	 * @param int $target
	 * @return float
	 */
	public static function billing_change(int $source,int $target): float
	{
		return Arr::get(self::billing_periods,$target.'.interval')/Arr::get(self::billing_periods,$source.'.interval');
	}

	/**
	 * Return the name for the billing interval
	 *
	 * @param int $interval
	 * @return string
	 */
	public static function billing_name(int $interval): string
	{
		$interval = collect(self::billing_periods)->get($interval);

		return Arr::get($interval,'name','Unknown');
	}

	/**
	 * Return the number of months in the billing interval
	 *
	 * @param int $interval
	 * @return int
	 */
	public static function billing_period(int $interval): int
	{
		$interval = collect(self::billing_periods)->get($interval);

		return Arr::get($interval,'interval',0);
	}

	/**
	 * Given a contract in months, this will calculate the number of billing intervals required
	 *
	 * @param int $contract_term
	 * @param int $source
	 * @return int
	 */
	public static function billing_term(int $contract_term,int $source): int
	{
		return ceil(($contract_term ?: 1)/(Arr::get(self::billing_periods,$source.'.interval') ?: 1));
	}

	/* INTERFACES */

	/**
	 * Invoice Local ID
	 *
	 * @return string
	 */
	public function getLIDAttribute(): string
	{
		return sprintf('%06s',$this->id);
	}

	/**
	 * Invoice System ID
	 *
	 * @return string
	 */
	public function getSIDAttribute(): string
	{
		return sprintf('%02s-%04s-%s',$this->site_id,$this->account_id,$this->getLIDAttribute());
	}

	/* RELATIONS */

	public function account()
	{
		return $this->belongsTo(Account::class);
	}

	public function items()
	{
		return $this->hasMany(InvoiceItem::class)
			->where('active',TRUE)
			->with(['taxes','product']);
	}

	public function payments()
	{
		return $this->hasManyThrough(Payment::class,PaymentItem::class,NULL,'id',NULL,'payment_id')
			->active();
	}

	public function paymentitems()
	{
		return $this->hasMany(PaymentItem::class);
	}

	/* SCOPES */

	/**
	 * Search for a record
	 *
	 * @param        $query
	 * @param string $term
	 * @return mixed
	 */
	public function scopeSearch($query,string $term)
	{
		return $query->where('id','like','%'.$term.'%');
	}

	/* ATTRIBUTES */

	/**
	 * Balance due on an invoice
	 * @return float
	 */
	public function getDueAttribute(): float
	{
		return sprintf('%3.2f',$this->getTotalAttribute()-$this->getPaidAttribute());
	}

	/**
	 * @return mixed
	 * @todo Change references to due_at to use due_date
	 */
	public function getDueDateAttribute(): Carbon
	{
		return $this->due_at;
	}

	/**
	 * Date the invoices was created
	 *
	 * @return Carbon
	 */
	public function getInvoiceDateAttribute(): Carbon
	{
		return $this->created_at;
	}

	// @todo Move this to a site configuration
	public function getInvoiceTextAttribute()
	{
		return sprintf('Thank you for using %s for your Internet Services.',config('site')->site_name);
	}

	/**
	 * Total of payments received for this invoice
	 * excluding pending payments
	 *
	 * @return float
	 */
	public function getPaidAttribute(): float
	{
		return $this->paymentitems
			->filter(function($item) { return ! $item->payment->pending_status && $item->payment->active; })
			->sum('amount');
	}

	/**
	 * Get the date that the invoice was paid in full.
	 * We assume the last payment received pays it in full, if its fully paid.
	 *
	 * @return Carbon|null
	 */
	public function getPaidDateAttribute(): ?Carbon
	{
		if ($this->getDueAttribute())
			return NULL;

		$o = $this->payments
			->filter(function($item) { return ! $item->pending_status; })
			->last();

		return $o?->paid_at;
	}

	/**
	 * Total of pending payments received for this invoice
	 *
	 * @return mixed
	 */
	public function getPaidPendingAttribute(): float
	{
		return $this->paymentitems
			->filter(function($item) { return $item->payment->pending_status; })
			->sum('amount');
	}

	/**
	 * Get invoice subtotal before taxes
	 *
	 * @return float
	 */
	public function getSubTotalAttribute(): float
	{
		return $this->items->where('active',TRUE)->sum('sub_total');
	}

	/**
	 * Get the invoices taxes total
	 *
	 * @return float
	 * @deprecated use getTotalTaxAttribute();
	 */
	public function getTaxTotalAttribute(): float
	{
		return $this->getTotalTaxAttribute();
	}

	/**
	 * Get the invoices taxes total
	 *
	 * @return float
	 */
	public function getTotalTaxAttribute(): float
	{
		return $this->items->where('active',TRUE)->sum('tax');
	}

	/**
	 * Invoice total due
	 *
	 * @return float
	 */
	public function getTotalAttribute(): float
	{
		return $this->getSubTotalAttribute()+$this->getTotalTaxAttribute();
	}

	/* METHODS */

	// @todo This shouldnt be here - current should be handled at an account level.
	public function currency()
	{
		return $this->account->country->currency;
	}

	/**
	 * Return a download link for non-auth downloads
	 *
	 * @return string
	 */
	public function download_link(): string
	{
		// Re-use an existing code
		$io = Invite::where('for',$this->account->user->email)->first();

		$tokendate = ($x=Carbon::now()->addDays(21)) > ($y=$this->due_at->addDays(21)) ? $x : $y;

		// Extend the expire date
		if ($io AND ($tokendate > $io->valid_until)) {
			$io->valid_until = $tokendate;
			$io->save();
		}

		$code = (! $io) ? Doorman::generate()->for($this->account->user->email)->uses(0)->expiresOn($tokendate)->make()->first()->code : $io->code;

		return url('u/invoice',[$this->id,'email',$code]);
	}

	// @todo document
	public function products()
	{
		$return = collect();

		foreach ($this->items->groupBy('product_id') as $o) {
			$po = $o->first()->product;
			$po->count = count($o->pluck('service_id')->unique());

			$return->push($po);
		}

		return $return->sortBy(function ($item) {
			return $item->name;
		});
	}

	// @todo document
	public function product_services(Product $po)
	{
		$return = collect();

		$this->items->load(['service']);

		foreach ($this->items->filter(function ($item) use ($po) {
			return $item->product_id == $po->id;
		}) as $o)
		{
			$so = $o->service;
			$return->push($so);
		};

		return $return->unique()->sortBy('name');
	}

	// @todo document
	public function product_service_items(Product $po,Service $so)
	{
		return $this->items->filter(function ($item) use ($po,$so) {
			return $item->product_id == $po->id AND $item->service_id == $so->id;
		})->filter()->sortBy('item_type');
	}

	/**
	 * @param string $key
	 * @return array
	 * @todo Ugly hack to update reminders
	 */
	public function reminders(string $key): array
	{
		$r = $this->reminders;
		if (! Arr::get($r,$key)) {
			$r[$key] = time();
		}

		return $r;
	}

	/**
	 * Automatically set our due_at at save time.
	 *
	 * @param array $options
	 * @return bool
	 */
	public function save(array $options = [])
	{
		// Automatically set the date_due attribute for new records.
		if (! $this->exists AND ! $this->due_at) {
			$this->due_at = $this->items->min('start_at');

			// @todo This 7 days should be sysetm configurable
			if (($x=Carbon::now()->addDay(7)) > $this->due_at)
				$this->due_at = $x;
		}

		return parent::save($options);
	}
}