<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Plan extends Model
{
	use HasFactory;
	
	protected $fillable = [
		'code',
		'plan_date',
		'status',
		'user_id',
		'is_activated',
		'is_deleted'
	];
	
	protected $casts = [
		'plan_date'    => 'date',
		'status'       => 'integer',
		'is_activated' => 'boolean',
		'is_deleted'   => 'boolean'
	];
	
	// Relationships
	public function planDetails()
	{
		return $this->hasMany(PlanDetail::class);
	}
	
	public function reports()
	{
		return $this->hasManyThrough(Report::class, PlanDetail::class);
	}
	
	public function user()
	{
		return $this->belongsTo(User::class);
	}
	
	// Scopes
	public function scopeActive($query)
	{
		return $query->where('is_activated', 1)->where('is_deleted', 0);
	}
	
	public function scopeByDate($query, $date)
	{
		return $query->where('plan_date', $date);
	}
	
	public function scopeByStatus($query, $status)
	{
		return $query->where('status', $status);
	}
	
	// Status constants
	const STATUS_DRAFT = 0;
	const STATUS_ACTIVE = 1;
	const STATUS_COMPLETED = 2;
	const STATUS_CANCELLED = 3;
	
	public static function getStatusLabels()
	{
		return [
			self::STATUS_DRAFT     => 'Draft',
			self::STATUS_ACTIVE    => 'Active',
			self::STATUS_COMPLETED => 'Completed',
			self::STATUS_CANCELLED => 'Cancelled'
		];
	}
	
	public function getStatusLabelAttribute()
	{
		$labels = self::getStatusLabels();
		return $labels[$this->status] ?? 'Unknown';
	}
}
