<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('employee_profiles', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('user_id')->constrained()->cascadeOnDelete();
            $t->string('position')->nullable();         // Mecanic / Maistru / Recepție / Magazioner
            $t->decimal('base_salary', 10, 2)->default(0);
            $t->decimal('works_pct', 5, 2)->default(0); // % din venitul manoperelor finalizate
            $t->decimal('parts_pct', 5, 2)->default(0); // % din marja pieselor vândute
            $t->date('hire_date')->nullable();
            $t->text('notes')->nullable();
            $t->timestamps();
            $t->softDeletes();

            $t->unique(['company_id', 'user_id']);
        });

        Schema::create('payroll_runs', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('user_id')->constrained()->cascadeOnDelete();
            $t->string('period', 7);                    // YYYY-MM
            $t->decimal('base', 10, 2)->default(0);
            $t->decimal('works_revenue', 12, 2)->default(0);
            $t->decimal('works_pct_amount', 10, 2)->default(0);
            $t->decimal('parts_margin', 12, 2)->default(0);
            $t->decimal('parts_pct_amount', 10, 2)->default(0);
            $t->decimal('bonus', 10, 2)->default(0);
            $t->decimal('fines', 10, 2)->default(0);
            $t->decimal('advance', 10, 2)->default(0);
            $t->decimal('total', 10, 2)->default(0);
            $t->boolean('paid')->default(false);
            $t->date('paid_at')->nullable();
            $t->text('notes')->nullable();
            $t->timestamps();

            $t->unique(['company_id', 'user_id', 'period']);
            $t->index(['company_id', 'period']);
        });

        Schema::create('payroll_adjustments', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('user_id')->constrained()->cascadeOnDelete();
            $t->string('type');                         // bonus / fine / advance
            $t->decimal('amount', 10, 2);
            $t->string('period', 7)->nullable();        // YYYY-MM (la care se aplică)
            $t->date('date')->default(now());
            $t->string('reason')->nullable();
            $t->boolean('applied')->default(false);     // marcat true după payroll run
            $t->timestamps();
            $t->softDeletes();

            $t->index(['company_id', 'user_id', 'period']);
            $t->index(['company_id', 'type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('payroll_adjustments');
        Schema::dropIfExists('payroll_runs');
        Schema::dropIfExists('employee_profiles');
    }
};
