<?php

namespace App\Models;

use App\Concerns\HasUuidV7;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Cashier\Billable;

class Workspace extends Model
{
    use Billable;
    use HasFactory;
    use HasUuidV7;
    use SoftDeletes;

    protected $fillable = [
        'name', 'slug', 'plan_id', 'owner_user_id',
        // Cashier's Billable trait writes Stripe customer/subscription
        // IDs into its own `stripe_id` + `subscriptions` table. The
        // pre-Cashier `stripe_customer_id` / `stripe_subscription_id`
        // columns are dead — keep them in DB for back-compat readers
        // but never mass-assign to them (silently no-op + would
        // confuse future maintainers into thinking they're live).
        'payment_gateway', 'paypal_subscription_id', 'razorpay_subscription_id',
        'settings', 'widget_defaults', 'live_chat_personalize',
        'slack_webhook_url', 'teams_webhook_url', 'business_hours',
    ];

    protected $casts = [
        'settings' => 'array',
        'widget_defaults' => 'array',
        'live_chat_personalize' => 'boolean',
        'business_hours' => 'array',
    ];

    public function plan(): BelongsTo
    {
        return $this->belongsTo(Plan::class);
    }

    public function owner(): BelongsTo
    {
        return $this->belongsTo(User::class, 'owner_user_id');
    }

    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'workspace_users')
            ->withPivot('role', 'invited_at', 'accepted_at')
            ->withTimestamps();
    }

    public function workspaceUsers(): HasMany
    {
        return $this->hasMany(WorkspaceUser::class);
    }

    public function agents(): HasMany
    {
        return $this->hasMany(Agent::class);
    }

    public function planSubscription(): HasOne
    {
        return $this->hasOne(PlanSubscription::class);
    }
}
