<?php

namespace App\Models;

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

class Activity extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'business_id',
        'project_id',
        'case_id',
        'contact_id',
        'lead_id',
        'deal_id',
        'ticket_id',
        'account_id',
        'activity_type_id',
        'title',
        'description',
        'location',
        'meeting_integration_id',
        'attachment',
        'start_date',
        'start_time',
        'end_date',
        'end_time',
        'all_day_eventstatus',
        'call_from',
        'phone_number',
        'fax',
        'address',
        'direction',
        'from_email',
        'email_to',
        'subject',
        'email_body',
        'email_attachment',
        'company_id',
        'first_name',
        'last_name',
        'duration',
        'priority_type_id',
        'due_date',
        'status_id',
        'owner_id',
        'status',
        'meeting_details',
        'participant_id',
        'hosting',
        'related_to_id',
        'repeat_type_id',
        'regarding',
        'service',
        'customer_id',
        'promotion_code',
        'related_campaign_id',
        'response_code',
        'channel_id',
        'received_on',
        'close_by',
        'lifecycle_status', 'outcome', 'meeting_type', 'agenda', 'remind_at',
        'visibility', 'to_recipients', 'cc_recipients', 'bcc_recipients',
        'email_status', 'scheduled_at', 'sent_at', 'opened_at', 'replied_at', 'category',
        'legacy_type', 'legacy_id', 'tracking_token', 'reply_token', 'calendar_uid',
        'external_calendar_id', 'calendar_sync_status', 'calendar_sync_error',
        'completed_at','attendee_result','next_action','next_action_type','no_next_action_reason','next_action_record_id',
        'meeting_location_type','meeting_link',
        'reminder_escalation_hours',
    ];

    protected function casts(): array
    {
        return [
            'to_recipients' => 'array', 'cc_recipients' => 'array', 'bcc_recipients' => 'array',
            'remind_at' => 'datetime', 'scheduled_at' => 'datetime', 'sent_at' => 'datetime',
            'opened_at' => 'datetime', 'replied_at' => 'datetime',
            'completed_at' => 'datetime',
        ];
    }

    public function assignments() { return $this->morphMany(CrmAssignment::class, 'assignable'); }
    public function account() { return $this->belongsTo(Account::class); }
    public function owner() { return $this->belongsTo(User::class, 'owner_id'); }
    public function ticket() { return $this->belongsTo(Ticket::class); }
    public function assignees() { return $this->morphToMany(User::class, 'assignable', 'crm_assignments')->withTimestamps(); }
    public function attendees() { return $this->belongsToMany(User::class, 'activity_attendees')->withPivot('response')->withTimestamps(); }
    public function files() { return $this->morphMany(CrmFile::class, 'fileable'); }
    public function statusHistory() { return $this->morphMany(CrmStatusHistory::class, 'statusable'); }
    public function trackingEvents() { return $this->hasMany(EmailTrackingEvent::class); }
}
