<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * PIPA / GDPR consent ledger.
 * Each row is immutable — new consent or withdrawal appends a new row.
 *
 * @property string $id
 * @property string $tenant_id
 * @property string $user_id
 * @property string $purpose       e.g. marketing, analytics, third_party_share
 * @property string $jurisdiction  PIPA | GDPR | CCPA
 * @property string $version       policy version, e.g. "2026-05-20"
 * @property bool $granted
 * @property string|null $ip
 * @property string|null $ua
 * @property \Illuminate\Support\Carbon $recorded_at
 */
class ConsentRecord extends Model
{
    use BelongsToTenant;
    use HasUuids;

    public const JURISDICTION_PIPA = 'PIPA';
    public const JURISDICTION_GDPR = 'GDPR';
    public const JURISDICTION_CCPA = 'CCPA';

    protected $fillable = [
        'tenant_id',
        'user_id',
        'purpose',
        'jurisdiction',
        'version',
        'granted',
        'ip',
        'ua',
        'recorded_at',
    ];

    protected $casts = [
        'granted' => 'boolean',
        'recorded_at' => 'datetime',
    ];

    public $timestamps = false;

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