"use strict";
/**
 * Contract Entity
 * Contratos con clientes y subcontratistas
 *
 * @module Contracts
 * @table contracts.contracts
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contract = void 0;
const typeorm_1 = require("typeorm");
const tenant_entity_1 = require("../../core/entities/tenant.entity");
const user_entity_1 = require("../../core/entities/user.entity");
const contract_addendum_entity_1 = require("./contract-addendum.entity");
let Contract = class Contract {
    id;
    tenantId;
    projectId;
    fraccionamientoId;
    contractNumber;
    contractType;
    clientContractType;
    name;
    description;
    // Client info (for client contracts)
    clientName;
    clientRfc;
    clientAddress;
    // Subcontractor info (for subcontractor contracts)
    subcontractorId;
    specialty;
    // Contract terms
    startDate;
    endDate;
    contractAmount;
    currency;
    paymentTerms;
    retentionPercentage;
    advancePercentage;
    // Status and workflow
    status;
    submittedAt;
    submittedById;
    legalApprovedAt;
    legalApprovedById;
    approvedAt;
    approvedById;
    signedAt;
    terminatedAt;
    terminationReason;
    // Documents
    documentUrl;
    signedDocumentUrl;
    // Progress tracking
    progressPercentage;
    invoicedAmount;
    paidAmount;
    notes;
    createdAt;
    createdById;
    updatedAt;
    updatedById;
    deletedAt;
    deletedById;
    // Computed properties
    get remainingAmount() {
        return Number(this.contractAmount) - Number(this.invoicedAmount);
    }
    get isExpiring() {
        const thirtyDaysFromNow = new Date();
        thirtyDaysFromNow.setDate(thirtyDaysFromNow.getDate() + 30);
        return this.endDate <= thirtyDaysFromNow && this.status === 'active';
    }
    // Relations
    tenant;
    createdBy;
    approvedBy;
    addendums;
};
exports.Contract = Contract;
__decorate([
    (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
    __metadata("design:type", String)
], Contract.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'tenant_id', type: 'uuid' }),
    __metadata("design:type", String)
], Contract.prototype, "tenantId", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'project_id', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "projectId", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'fraccionamiento_id', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "fraccionamientoId", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'contract_number', type: 'varchar', length: 50 }),
    __metadata("design:type", String)
], Contract.prototype, "contractNumber", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'contract_type', type: 'varchar', length: 20 }),
    __metadata("design:type", String)
], Contract.prototype, "contractType", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'client_contract_type', type: 'varchar', length: 30, nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "clientContractType", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'varchar', length: 255 }),
    __metadata("design:type", String)
], Contract.prototype, "name", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'text', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "description", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'client_name', type: 'varchar', length: 255, nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "clientName", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'client_rfc', type: 'varchar', length: 13, nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "clientRfc", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'client_address', type: 'text', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "clientAddress", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'subcontractor_id', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "subcontractorId", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'specialty', type: 'varchar', length: 50, nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "specialty", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'start_date', type: 'date' }),
    __metadata("design:type", Date)
], Contract.prototype, "startDate", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'end_date', type: 'date' }),
    __metadata("design:type", Date)
], Contract.prototype, "endDate", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'contract_amount', type: 'decimal', precision: 16, scale: 2 }),
    __metadata("design:type", Number)
], Contract.prototype, "contractAmount", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'varchar', length: 3, default: 'MXN' }),
    __metadata("design:type", String)
], Contract.prototype, "currency", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'payment_terms', type: 'text', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "paymentTerms", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'retention_percentage', type: 'decimal', precision: 5, scale: 2, default: 5 }),
    __metadata("design:type", Number)
], Contract.prototype, "retentionPercentage", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'advance_percentage', type: 'decimal', precision: 5, scale: 2, default: 0 }),
    __metadata("design:type", Number)
], Contract.prototype, "advancePercentage", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'varchar', length: 20, default: 'draft' }),
    __metadata("design:type", String)
], Contract.prototype, "status", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'submitted_at', type: 'timestamptz', nullable: true }),
    __metadata("design:type", Date)
], Contract.prototype, "submittedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'submitted_by', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "submittedById", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'legal_approved_at', type: 'timestamptz', nullable: true }),
    __metadata("design:type", Date)
], Contract.prototype, "legalApprovedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'legal_approved_by', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "legalApprovedById", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'approved_at', type: 'timestamptz', nullable: true }),
    __metadata("design:type", Date)
], Contract.prototype, "approvedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'approved_by', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "approvedById", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'signed_at', type: 'timestamptz', nullable: true }),
    __metadata("design:type", Date)
], Contract.prototype, "signedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'terminated_at', type: 'timestamptz', nullable: true }),
    __metadata("design:type", Date)
], Contract.prototype, "terminatedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'termination_reason', type: 'text', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "terminationReason", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'document_url', type: 'varchar', length: 500, nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "documentUrl", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'signed_document_url', type: 'varchar', length: 500, nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "signedDocumentUrl", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'progress_percentage', type: 'decimal', precision: 5, scale: 2, default: 0 }),
    __metadata("design:type", Number)
], Contract.prototype, "progressPercentage", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'invoiced_amount', type: 'decimal', precision: 16, scale: 2, default: 0 }),
    __metadata("design:type", Number)
], Contract.prototype, "invoicedAmount", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'paid_amount', type: 'decimal', precision: 16, scale: 2, default: 0 }),
    __metadata("design:type", Number)
], Contract.prototype, "paidAmount", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'text', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "notes", void 0);
__decorate([
    (0, typeorm_1.CreateDateColumn)({ name: 'created_at', type: 'timestamptz' }),
    __metadata("design:type", Date)
], Contract.prototype, "createdAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'created_by', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "createdById", void 0);
__decorate([
    (0, typeorm_1.UpdateDateColumn)({ name: 'updated_at', type: 'timestamptz' }),
    __metadata("design:type", Date)
], Contract.prototype, "updatedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'updated_by', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "updatedById", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'deleted_at', type: 'timestamptz', nullable: true }),
    __metadata("design:type", Date)
], Contract.prototype, "deletedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({ name: 'deleted_by', type: 'uuid', nullable: true }),
    __metadata("design:type", String)
], Contract.prototype, "deletedById", void 0);
__decorate([
    (0, typeorm_1.ManyToOne)(() => tenant_entity_1.Tenant),
    (0, typeorm_1.JoinColumn)({ name: 'tenant_id' }),
    __metadata("design:type", tenant_entity_1.Tenant)
], Contract.prototype, "tenant", void 0);
__decorate([
    (0, typeorm_1.ManyToOne)(() => user_entity_1.User),
    (0, typeorm_1.JoinColumn)({ name: 'created_by' }),
    __metadata("design:type", user_entity_1.User)
], Contract.prototype, "createdBy", void 0);
__decorate([
    (0, typeorm_1.ManyToOne)(() => user_entity_1.User),
    (0, typeorm_1.JoinColumn)({ name: 'approved_by' }),
    __metadata("design:type", user_entity_1.User)
], Contract.prototype, "approvedBy", void 0);
__decorate([
    (0, typeorm_1.OneToMany)(() => contract_addendum_entity_1.ContractAddendum, (a) => a.contract),
    __metadata("design:type", Array)
], Contract.prototype, "addendums", void 0);
exports.Contract = Contract = __decorate([
    (0, typeorm_1.Entity)({ schema: 'contracts', name: 'contracts' }),
    (0, typeorm_1.Index)(['tenantId', 'contractNumber'], { unique: true })
], Contract);
//# sourceMappingURL=contract.entity.js.map