<?php

namespace App\Models;

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

class Company extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'logo',
        'secondary_logo',
        'project_name',
        'code',
        'address',
        'city',
        'phone',
        'email',
        'latitude',
        'longitude',
        'radius',
        'work_start_time',
        'work_end_time',
        'is_active',
        'bypass_claim_approval',
        'allow_editable_location',
        'device_validation_enabled',
        'max_devices_per_user',
        'allow_document_upload',
        'backdate_enabled',
        'allow_business_trip',
        'attendance_auto_enabled',
        'office_time_enabled',
        'late_tolerance_minutes',
        'weekend_days',
        'alarm_enabled',
        'checkin_reminder_time',
        'checkout_reminder_after_hours',
        'overtime_enabled',
        'overtime_auto_approval',
        'minimum_overtime_minutes',
        'support_phone',
        'support_email',
        'team_leader_name',
        'team_leader_position',
        'honorarium_per_day',
        'qr_signing_secret',
        'qr_card_enabled',
        'qr_scanner_enabled',
        'qr_scanner_radius',
        'activity_mandatory_enabled',
        'activity_mandatory_scope',
        'allow_employee_edit_home_location',
        'show_recap_for_admin',
        'show_recap_for_employee',
    ];

    protected function casts(): array
    {
        return [
            'latitude' => 'decimal:8',
            'longitude' => 'decimal:8',
            'radius' => 'integer',
            'work_start_time' => 'datetime:H:i',
            'work_end_time' => 'datetime:H:i',
            'is_active' => 'boolean',
            'bypass_claim_approval' => 'boolean',
            'allow_editable_location' => 'boolean',
            'device_validation_enabled' => 'boolean',
            'max_devices_per_user' => 'integer',
            'allow_document_upload' => 'boolean',
            'backdate_enabled' => 'boolean',
            'allow_business_trip' => 'boolean',
            'attendance_auto_enabled' => 'boolean',
            'office_time_enabled' => 'boolean',
            'late_tolerance_minutes' => 'integer',
            'weekend_days' => 'array',
            'alarm_enabled' => 'boolean',
            'checkin_reminder_time' => 'datetime:H:i',
            'checkout_reminder_after_hours' => 'integer',
            'overtime_enabled' => 'boolean',
            'overtime_auto_approval' => 'boolean',
            'minimum_overtime_minutes' => 'integer',
            'honorarium_per_day' => 'decimal:2',
            'qr_card_enabled' => 'boolean',
            'qr_scanner_enabled' => 'boolean',
            'qr_scanner_radius' => 'integer',
            'activity_mandatory_enabled' => 'boolean',
            'allow_employee_edit_home_location' => 'boolean',
            'show_recap_for_admin' => 'boolean',
            'show_recap_for_employee' => 'boolean',
        ];
    }

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

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

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

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

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

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

    /**
     * Get the default shift for this company
     */
    public function defaultShift()
    {
        return $this->shifts()->where('is_default', true)->first();
    }

    /**
     * Get holidays for this company
     */
    public function holidays(): HasMany
    {
        return $this->hasMany(Holiday::class);
    }

    /**
     * Get overtimes for this company
     */
    public function overtimes(): HasMany
    {
        return $this->hasMany(Overtime::class);
    }

    /**
     * Get employee cards for this company
     */
    public function employeeCards(): HasMany
    {
        return $this->hasMany(EmployeeCard::class);
    }

    /**
     * Get activity assignment rules for this company
     */
    public function activityAssignmentRules(): HasMany
    {
        return $this->hasMany(ActivityAssignmentRule::class);
    }

    /**
     * Get QR attendance logs for this company
     */
    public function qrAttendanceLogs(): HasMany
    {
        return $this->hasMany(QrAttendanceLog::class);
    }

    /**
     * Check if a given date is a weekend day
     */
    public function isWeekend($date): bool
    {
        $dayOfWeek = is_string($date) ? \Carbon\Carbon::parse($date)->dayOfWeek : $date->dayOfWeek;
        $weekendDays = $this->weekend_days ?? [0, 6]; // Default: Minggu (0) dan Sabtu (6)

        return in_array($dayOfWeek, $weekendDays);
    }

    /**
     * Check if a given date is a holiday (weekend or tanggal merah)
     */
    public function isHoliday($date): bool
    {
        // Check if weekend
        if ($this->isWeekend($date)) {
            return true;
        }

        // Check if tanggal merah
        return Holiday::isHoliday($date, $this->id);
    }

    /**
     * Get weekend days as labels
     */
    public function getWeekendLabelsAttribute(): array
    {
        $days = ['Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu'];
        $weekendDays = $this->weekend_days ?? [0, 6];

        return array_map(fn($d) => $days[$d], $weekendDays);
    }
}
